Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Instantiated Object supposedly null when added to an array

Discussion in 'Scripting' started by AllyPallyUK, Dec 1, 2023.

  1. AllyPallyUK

    AllyPallyUK

    Joined:
    Feb 29, 2020
    Posts:
    4
    Hi there, this is probably a simple fix but I can't find a solution

    I'm just trying to instantiate a gameobject multiple times into a grid shape using two for loops. When I try to add the instantiated object to a 2d array however, an error is thrown saying the object reference is not set to an instance of an object, despite my Debug.Log(object) clearly displaying the object is there?
    Code (CSharp):
    1.  
    2. public GameObject prefab;
    3. pubic Collider2D[,] Colliders;
    4. public int gridWidth = 6;
    5. public int Gridheight = 8;
    6.  
    7. void Start()
    8.     {
    9.         for (int x = 0; x < gridWidth; x++)
    10.         {
    11.             for (int y = 0; y < gridHeight; y++)
    12.             {
    13.                 Collider2D newCollider = Instantiate(prefab, transform.position + new Vector3(x, y, 0), transform.rotation).GetComponent<Collider2D>();
    14.                 Debug.Log(newCollider);
    15.                 Colliders[x, y] = (BoxCollider2D)newCollider;
    16.             }
    17.         }
    18.         Debug.Log(Colliders);
    19.     }
    Any help would be massively appreciated, and any extra info you need I can happily provide. I'll be active for the next few hours :)
    upload_2023-12-1_20-47-11.png

    upload_2023-12-1_20-47-27.png
     

    Attached Files:

  2. AllyPallyUK

    AllyPallyUK

    Joined:
    Feb 29, 2020
    Posts:
    4
    Here is the object I'm using as a prefab
    upload_2023-12-1_20-49-30.png
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,272
    Then you didn't find the thing that is null!!

    Is it your array? You didn't post code showing how that gets initialized.

    Also it says line 36 but you only posted a small snippet, so go back to line 36 and look again.

    Go back to step #1.

    The answer is always the same... ALWAYS!

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that
     
    AllyPallyUK likes this.
  4. AllyPallyUK

    AllyPallyUK

    Joined:
    Feb 29, 2020
    Posts:
    4
    This was perfect and helped solve my issue, thank you so much.

    For any future users, it turns out in my 2D Array definiton I hadn't defined the dimensions of the array and thus it was just empty, and so when I tried to assign the instantiated objects to a null array things went boom. Thank you again ^u^
     
    Kurt-Dekker likes this.
  5. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    591
    It's your array that is null, not the collider.

    Edit: Sorry didn't see that you found the problem.