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

GameObject array return NullReferenceException... why?

Discussion in 'Scripting' started by HAlbera, Oct 6, 2014.

  1. HAlbera

    HAlbera

    Joined:
    Jun 7, 2013
    Posts:
    63
    Hey, I know this has probably been answered before but I searched and can't seem to work out why my check isn't working.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GridSys : MonoBehaviour {
    5.  
    6.     GameObject test;
    7.  
    8.     public GameObject[,] Grid;
    9.  
    10.     //return true if empty
    11.     bool CheckEmpty (int x, int y){
    12.         Debug.Log (Grid[x,y]);
    13.  
    14. //THIS IS WHERE I AM GETTING A NullReferenceException at line 12.
    15.  
    16.         if (Grid[x,y] == null){
    17.             return true;
    18.         }
    19.         else {
    20.             return false;
    21.         }
    22.     }
    23.  
    24.     //return true on success
    25.     bool Add (int x, int y, GameObject a){
    26.         if (CheckEmpty (x,y)){
    27.             Grid[x,y] = a;
    28.             return true;
    29.         }
    30.         else {
    31.             return false;
    32.         }
    33.     }
    34.  
    35.     void Start () {
    36.         test = GameObject.Find("TestObject");
    37.         Debug.Log (test);
    38.         Add (1,1,test);
    39.         Debug.Log ("Added " + test);
    40.     }
    41.  
    42.     void Update () {
    43.  
    44.     }
    45.  
    46.  
    47. }
    From my understanding I have created an array of GameObjects with GameObject[,].

    In Start() I am finding the test object I have in scene.
    Using Add()
    In turn uses CheckEmpty()
    Now CheckEmpty should return true to the (GameObject[x,y] == null) and proceed with Grid[x,y] = a?

    The problem as I see it is that the actual check on the array is failing because the array position doesn't exist, do I need to add a for loop in there and initialise the amount of positions I need?

    This is for a grid based game. This script will just handle a few grid operations like checking if positions are empty and holding an array of GameObjects in those positions and returning GameObject references etc, nothing more. IT would be nice to not have to use an array of a definate size but i dont intend to have infinite worlds so i guess its not such a problem.

    Many thanks.

    Halbera.

    EDIT: I do realise I will need to check if that position has a gameobject in it aswell as if it is null, but for simplicities sake im leaving it empty first of all.
     
  2. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
  3. HAlbera

    HAlbera

    Joined:
    Jun 7, 2013
    Posts:
    63
    Okay I had guessed that. So do I need to init every position I might possibly use in the future?

    Thankyou for the reply.
     
  4. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    No, not explicitly. You just initialize the variable with a new instance of an array with given amount of elements.

    Anwser to that question is right there in resource I provided. How about you read it and learn how to use arrays. Better yet how about you go learn about any specific aspect of C# language you want to use before asking people here to explain it to you because you don't feel like doing the work yourself.

    People Don't get paid for anwsering your question here, you know that right?
     
  5. HAlbera

    HAlbera

    Joined:
    Jun 7, 2013
    Posts:
    63
    People don't get paid for using manners either, but they still do.

    Maybe I should drill down to my question a little more.

    I read the resource you linked, thank-you. Now the question I would like to know is are C# arrays resizable or when I declare and init this array using GameObject Grid[,] = new GameObject[9,9]; am I now limited to a 10x10 array?

    Thank-you again.
     
  6. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    No, arrays are not resizable: https://www.google.com/#q=are+c#+arrays+resizable&spell=1
    You should use generic lists instead that will allow you to change size dynamically.

    Expending on previous point: since your array is of reference types ( GameObject ) then refering to specific element without initializing it first will also produce null reference exception. So after initializing the array itself it's safe to reference the array but not referencing specific element untill it too was initialized.
     
  7. HAlbera

    HAlbera

    Joined:
    Jun 7, 2013
    Posts:
    63
    Ah okay brilliant, thank-you. So I assume after initializing the array I will first have to check the index for a null value prior to attempting to access the gameobject stored.

    Thanks for the extra info and reply.

    Halbera
     
  8. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    Exactly.