Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Why when using array of Renderer i'm getting null reference exception ?

Discussion in 'Scripting' started by Chocolade, Aug 1, 2017.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    932
    In the top of the script i have:

    Code (csharp):
    1.  
    2. [SerializeField] public Transform[] m_PropModel;
    3. [SerializeField] public Transform[] m_PropBlur;
    4. private Renderer[] m_PropellorModelRenderer;
    5. private Renderer[] m_PropellorBlurRenderer;
    6. private Renderer test;
    7.  
    Then in Awake()

    Code (csharp):
    1.  
    2. private void Awake()
    3.         {
    4.             // Set up the reference to the aeroplane controller.
    5.             m_Plane = GetComponent<AeroplaneController>();
    6.  
    7.             test = m_PropModel[0].GetComponent<Renderer>();
    8.             m_PropellorModelRenderer[0] = m_PropModel[0].GetComponent<Renderer>();
    9.             m_PropellorBlurRenderer[0] = m_PropBlur[0].GetComponent<Renderer>();
    10.             m_PropellorModelRenderer[1] = m_PropModel[1].GetComponent<Renderer>();
    11.             m_PropellorBlurRenderer[1] = m_PropBlur[1].GetComponent<Renderer>();
    12.  
    13.             m_PropBlur[0].parent = m_PropModel[0];
    14.             m_PropBlur[1].parent = m_PropModel[1];
    15.         }
    16.  
    When i'm using the test variable it will be fine.
    But when i'm using the m_PropellorModelRenderer[[0] the m_PropellorModelRenderer[ will be null.
    Why when it's in Array it's null but when it's alone single Renderer it's not null ?
    How can i fix it ?

    NullReferenceException: Object reference not set to an instance of an object
    (wrapper stelemref) object:stelemref (object,intptr,object)

    At line 34
    Line 34 is:

    Code (csharp):
    1.  
    2. m_PropellorModelRenderer[0] = m_PropModel[0].GetComponent<Renderer>();
    3.  
    test is not null but m_PropellorModelRenderer[0] is null.

    I Attached to Unity and used breakpoint on this line.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    You have not initialized the array before you started populating it.
    Code (csharp):
    1. m_PropModelRenderer = new Renderer[m_PropModel.Length];
    And the same for any array that is not filled in the inspector.

    One additional note: any reason you're not using a for loop to process the array? Kind of pointless to use an array if you're not going to loop through it.
     
    cstooch and Chocolade like this.
  3. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    If you're referring to an actual index like you are here, I think you have to first size the array.. something like this....

    if you're expecing an array of 2 rows..

    Code (csharp):
    1. private Renderer[] someRendererArray = new Renderer[2];
    Edit: @StarManta beat me to it, and his answer is better and better explained.
     
    Chocolade likes this.