Search Unity

NullReference Error after Instantiating a Prefab

Discussion in 'Scripting' started by VincentBlaser, Jul 16, 2018.

  1. VincentBlaser

    VincentBlaser

    Joined:
    Jan 23, 2018
    Posts:
    3
    So I don´t know if I´m missing an dumb error of myself but my problem is:
    I´ve got an class which deserializes a json file into a tree-structure. That part is working without an error. Now while doing this i want to create Objects of another class - we just call it "CityObject" for the moment.
    Code (CSharp):
    1. public class CityObject : MonoBehaviour {
    2.  
    3.     private GameObject gameObject;
    4.  
    5.     public ArrayList children;
    6.  
    7.     public int width;
    8.  
    9.  
    10.     public CityObject(GameObject prefab, int newWidth)
    11.     {
    12.         gameObject = Instantiate(prefab);
    13.         children = new ArrayList();
    14.         width = newWidth;
    15.     }
    16. }
    Depending on the Information in the json file, the GameObject should get a simple cube prefab or a flat cube prefab and instantiate it. So far so good - in the unity Scene all Objects are created, so in my example there are 5 created GameObjects - 2 flat and 3 simple cubes.
    Code (CSharp):
    1. public class CityBuilder : MonoBehaviour {
    2.  
    3.     public GameObject plane;
    4.     public GameObject cube;
    5.     public CityObject origin;
    6.  
    7.     private int maxW = 0;
    8.  
    9.     private ProjectView ProcessProjectData(string jsonString)
    10.     {
    11.         ProjectView parsejson = JsonUtility.FromJson<ProjectView>(jsonString);
    12.         return parsejson;
    13.     }
    14.  
    15.     private GameObject FindPrefab(string type)
    16.     {
    17.         switch (type.ToLower())
    18.         {
    19.             case "block": newPlane = false; return cube;
    20.             case "scope": newPlane = true; return plane;
    21.             default: return null;
    22.         }
    23.     }
    24.    
    25.     private CityObject CreateGameObjectFromProjectData(ProjectData projectData, CityObject cityObjectParent)
    26.     {
    27.      
    28.         var prefab = FindPrefab(projectData.type);
    29.         if (!prefab)
    30.             return null;
    31.  
    32.  
    33.         CityObject rootCityObject = new CityObject(prefab,  getSubtreeWidth(projectData));
    34.  
    35.        
    36.         if(maxW == 0)
    37.         {
    38.             maxW = rootCityObject.width;
    39.         }
    40.         scaleObject(rootCityObject);
    41.        
    42.         /* * *
    43.          * If an object has one or more children, then the children should execute this method on their own.
    44.          */
    45.  
    46.         if (projectData.children != null)
    47.         {
    48.             foreach (ProjectData data in projectData.children)
    49.             {
    50.                 Debug.Log(data.id);
    51.             }
    52.             foreach (ProjectData data in projectData.children)
    53.             {
    54.                 CityObject newChild = CreateGameObjectFromProjectData(data, rootCityObject);
    55.                 rootCityObject.appendChild(newChild);
    56.             }
    57.         }
    58.         return rootCityObject;
    59.     }
    The problem is that in line 40 i want to scale the gameObject of the cityObject like this:

    private void scaleObject(CityObject node)
    {
    float scaleRatio = (float)(node.width/maxW);
    node.gameObject.transform.localScale.Set(scaleRatio, scaleRatio, scaleRatio);
    }

    Then there is a NullReference Error for the node.GameObject - so there seems to be no reference between the cityObject and the GameObject. This is the part i can´t understand. Is it the Instantiate in the CityObejct Constructor? Isn´t the Instantiate creating a Reference between the "Script-Object" and the Gameobject in the unity-Scence?
     
  2. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    450
    Nevermind, I was wrong. Sorry :-D