Search Unity

Add an instantiation of a class to a GameObject ?

Discussion in 'Scripting' started by Zavalichi, Oct 19, 2018.

  1. Zavalichi

    Zavalichi

    Joined:
    Oct 13, 2018
    Posts:
    162
    Hi guys,

    I created a script that generates a number of cars (gameObjects), then for each object of the Cars class add the car, the road it has to go through and the index showing where it arrived.
    In the Update method from the script, I have a for that calls a function for the movement of each machine.

    I'm sure what I did is not good, even if it works, so I created a script CarManager, but I don't want to read for every car my XML with all path. Can I created a Class (GlobalData) and read there from XML and after that to use it in my CarManager?

    I tried something like this, but i get Null exception..

    upload_2018-10-19_20-17-56.png
     
  2. barskey

    barskey

    Joined:
    Nov 19, 2017
    Posts:
    207
    GetComponent looks for a component attached to the game object. I am assuming since you will use this GlobalData for all gameobjects that you don't want to attach it to each one?

    Try using a Scriptable Object (basically a data container). Change the inheritance of GlobalData from MonoBehaviour to ScriptableObject. Then you could add a reference in the inspector to this scriptable object for each car, or better yet on the car prefab.

    Edit: Or make it a class without MonoBehaviour inheritance and you can access is from anywhere using GlobalData.methodname().
     
  3. Zavalichi

    Zavalichi

    Joined:
    Oct 13, 2018
    Posts:
    162
    It is attached to one Game Object.

    I tried what you say but doesn't work.
     
  4. barskey

    barskey

    Joined:
    Nov 19, 2017
    Posts:
    207
    You'll need to be more specific. Show what you tried, and explain why/how it didn't work.
     
  5. Zavalichi

    Zavalichi

    Joined:
    Oct 13, 2018
    Posts:
    162
    upload_2018-10-19_23-38-0.png

    Error: null

    But I think it is not a good idea because if I do ... = new GlobalData() for every car that means I read the XML everytime
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I'm not sure why you're being evasive with your explanations, but if you are hitting an error please post the error in its entirety and exactly what line the error occurs on. Also don't post pictures of your code, paste the code into the forum using CODE tags.
     
  7. Zavalichi

    Zavalichi

    Joined:
    Oct 13, 2018
    Posts:
    162
    Sorry, I was too tired last night.
    I have my class GlobalData which read my XML.

    Code (CSharp):
    1. public class GlobalData
    2. {
    3.  
    4.     [Tooltip("The resource file that contains the OSM map data")]
    5.     public string routesXML;
    6.  
    7.     [Tooltip("The resource file that contains the OSM map data")]
    8.     public string graphXML;
    9.  
    10.     // Graful cu nodurile rutelor
    11.     public List<Node> Graph;
    12.  
    13.     // Toate rutele generate pentru hartă
    14.     public List<Route> Routes;
    15.  
    16.  
    17.     GlobalData()
    18.     {
    19.         var txtAsset1 = Resources.Load<TextAsset>(routesXML);
    20.         var txtAsset2 = Resources.Load<TextAsset>(graphXML);
    21.  
    22.         XmlDocument doc1 = new XmlDocument();
    23.         doc1.LoadXml(txtAsset1.text);
    24.         Routes = readAllRoutes(doc1.SelectNodes("allPath/path"));
    25.  
    26.         XmlDocument doc2 = new XmlDocument();
    27.         doc2.LoadXml(txtAsset2.text);
    28.         Graph = GetGraph(doc2.SelectNodes("graph/node"));
    29.     }
    30.  
    31.     public List<Node> GetGraph(XmlNodeList nodeList)
    32.     {
    33.         List<Node> graph = new List<Node>();
    34.  
    35.         foreach (XmlNode node in nodeList)
    36.         {
    37.             Node nd = new Node();
    38.             nd.ID = System.Convert.ToUInt64(node.Attributes["id"].Value);
    39.  
    40.             Vector3 point = new Vector3();
    41.             point.x = (float)System.Convert.ToDouble((node.Attributes["x"].Value));
    42.             point.y = (float)System.Convert.ToDouble((node.Attributes["y"].Value));
    43.             point.z = (float)System.Convert.ToDouble((node.Attributes["z"].Value));
    44.             nd.Point = point;
    45.  
    46.             XmlNodeList connection = node.SelectNodes("connection");
    47.             foreach (XmlNode c in connection)
    48.             {
    49.                 Node con = new Node();
    50.                 con.ID = System.Convert.ToUInt64(c.Attributes["id"].Value);
    51.  
    52.                 point.x = (float)System.Convert.ToDouble((c.Attributes["x"].Value));
    53.                 point.y = (float)System.Convert.ToDouble((c.Attributes["y"].Value));
    54.                 point.z = (float)System.Convert.ToDouble((c.Attributes["z"].Value));
    55.                 con.Point = point;
    56.  
    57.                 nd.Connections.Add(con);
    58.             }
    59.  
    60.             graph.Add(nd);
    61.         }
    62.         return graph;
    63.     }
    64.     public List<Route> readAllRoutes(XmlNodeList routesXML)
    65.     {
    66.         //List<List<Node>> allPath = new List<List<Node>>();
    67.         List<Route> routes = new List<Route>();
    68.  
    69.         foreach (XmlNode p in routesXML)
    70.         {
    71.             Route route = new Route();
    72.             route.OneWay = System.Convert.ToString(p.Attributes["oneway"].Value);
    73.             route.StartID = System.Convert.ToUInt64(p.Attributes["startId"].Value);
    74.             route.EndID = System.Convert.ToUInt64(p.Attributes["endId"].Value);
    75.             route.Nr = System.Convert.ToInt16(p.Attributes["nr"].Value);
    76.  
    77.             XmlNodeList positions = p.SelectNodes("position");
    78.             foreach (XmlNode position in positions)
    79.             {
    80.                 Node node = new Node();
    81.  
    82.                 Vector3 point = new Vector3();
    83.                 point.x = (float)System.Convert.ToDouble((position.Attributes["x"].Value));
    84.                 point.y = (float)System.Convert.ToDouble((position.Attributes["y"].Value));
    85.                 point.z = (float)System.Convert.ToDouble((position.Attributes["z"].Value));
    86.                 node.Point = point;
    87.                 node.ID = System.Convert.ToUInt64(position.Attributes["id"].Value);
    88.  
    89.                 route.Nodes.Add(node);
    90.             }
    91.  
    92.             routes.Add(route);
    93.         }
    94.         return routes;
    95.     }
    96.  
    97.     public List<Node> GetGraph()
    98.     {
    99.         return Graph;
    100.     }
    101. }
    102.  
    In CarManager (the script for every car) I use:
    Code (CSharp):
    1. private void Awake()
    2.     {
    3.         allData =new GlobalData();
    4.  
    5.         Graph = allData.GetGraph();
    6.     }
    7.  
    ERROR:

    NullReferenceException: Object reference not set to an instance of an object
    CarManager.Start
     
  8. Zavalichi

    Zavalichi

    Joined:
    Oct 13, 2018
    Posts:
    162
    SOLVED

    Code (CSharp):
    1. public class GlobalData: MonoBehaviour
    2. {
    3.  
    4.     [Tooltip("The resource file that contains the OSM map data")]
    5.     public string routesXML;
    6.  
    7.     [Tooltip("The resource file that contains the OSM map data")]
    8.     public string graphXML;
    9.  
    10.     // Graful cu nodurile rutelor
    11.     public List<Node> Graph;
    12.  
    13.     // Toate rutele generate pentru hartă
    14.     public List<Route> Routes;
    15.  
    16.     // xml is ready
    17.     public bool dataIsReady = false;
    18.  
    19.     void Awake()
    20.     {
    21.         var txtAsset1 = Resources.Load<TextAsset>(routesXML);
    22.         var txtAsset2 = Resources.Load<TextAsset>(graphXML);
    23.  
    24.         XmlDocument doc1 = new XmlDocument();
    25.         doc1.LoadXml(txtAsset1.text);
    26.         Routes = readAllRoutes(doc1.SelectNodes("allPath/path"));
    27.  
    28.         XmlDocument doc2 = new XmlDocument();
    29.         doc2.LoadXml(txtAsset2.text);
    30.         Graph = GetGraph(doc2.SelectNodes("graph/node"));
    31.  
    32.         dataIsReady = true;
    33. .......
    34.     }
    In script which is attached to car I put:

    Code (CSharp):
    1.     void Start()
    2.     {
    3.         GameObject mp = GameObject.Find("Map");
    4.         Graph = mp.GetComponent<GlobalData>().Graph;
    5.         Routes = mp.GetComponent<GlobalData>().Routes;
    6.     ......
    The error was generated because I forgod to find the initial GameObject with DataGlobal script.