Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

What is wrong with my code?

Discussion in 'Scripting' started by surajsirohi1008, Aug 14, 2018.

  1. surajsirohi1008

    surajsirohi1008

    Joined:
    Jun 21, 2016
    Posts:
    266
    It's just a simple script, I've created an array of transforms and I've assigned the transforms in the editor, when game starts I want to print the name of first transform in the array but I get an error: "NullReferenceException: Object reference not set to an instance of an object"

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ManagerScript : MonoBehaviour {
    4.  
    5.     [SerializeField]
    6.     Transform[] treeHolders;
    7.  
    8.     // Use this for initialization
    9.  
    10.     private void Awake()
    11.     {
    12.      
    13.         treeHolders = new Transform[5];
    14.  
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.  
    20.         Debug.Log(treeHolders[0].name);
    21.        
    22.     }
    23. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    It's an array of Transform "spaces", but there isn't anything in there.
    When you make an array of anything, it just says, hey, I have space to hold something, now you need to put something in that space.

    So in your case, you have a hotel that has 5 rooms that can hold transforms. But those rooms are empty, so the value returned is null as there isn't anything inside the room. (one way to look at it)
     
  3. surajsirohi1008

    surajsirohi1008

    Joined:
    Jun 21, 2016
    Posts:
    266
    I've assigned the transforms in the editor, I even printed the length of the array which it is showing 5.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    You are overwriting it in Awake, so your values no longer exist.

    Remove your line in Awake.
     
  5. surajsirohi1008

    surajsirohi1008

    Joined:
    Jun 21, 2016
    Posts:
    266
    I'm new to programming, I learnt that we initialise an array by using the new keyword, right?
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Yes, but you have to understand Unity works a bit different.

    If you declare a collection (like an array) and then go into the inspector and assign values to it, Unity handles initializing it for you. If it didn't do that, you'd run into errors. It basically sets it up for you so it can add values to it.

    If you're creating an array yourself and not accessing it in the inspector, you would have to use new to create a new empty array.
     
    surajsirohi1008 likes this.
  7. surajsirohi1008

    surajsirohi1008

    Joined:
    Jun 21, 2016
    Posts:
    266
    oooo
    ooh, so the line "treeHolders = new Transform[5];" is already being handled by unity and I should remove that line from my script.
     
  8. surajsirohi1008

    surajsirohi1008

    Joined:
    Jun 21, 2016
    Posts:
    266
    It worked! Thanks mate!