- Home /
Init code goes to Null unknow reason
Hello Unity Community,
This is very basic question and i feel ashamed to ask this, but i feel i'm missing something. I'm simply initializing my grid on Start(), however when i try to acces it from addBuilding() it returns null.
Start() is the only place where the grid is assigned. It seems so simpel, but because it is so simple quite hard.
public int rectangles_x, rectangles_z;
private GameObject[,] grid
void Start () {
grid = new GameObject[rectangles_z , rectangles_x];
// A debug log returns the right 2D array
}
public void addBuilding(int x, int z, GameObject obj) {
Debug.Log (this.grid); // This returns Null
//grid [z, x] = obj; //This part gives "NullReferenceException" because grid == null
}
Hopefully anyone can help me.
With kind regards, Nkmol
Read about multidimensional arrays to understand how they are initialized and populated.
What are rectangles_z and rectangles_x?
@HarsHad$$anonymous$$ I don't think my syntax is wrong? rectangles_z and rectangles_x are variables assigned through unity GUI.
Answer by Nkmol1 · Feb 09, 2015 at 06:39 PM
it was my Script Excution Order. Apperently Start() wasn't called first. I feel so stupid ^^ Thanks for the help all!
Answer by stblue · Feb 09, 2015 at 01:34 PM
Its returning null because you have just supply the dimension and by default it will get initialize to "null" and since its not pointing to any objects right now, which means its value is null.
This is what i suspected. Howerver i thought C# supported null as objects. Like GameObject obj = null;
is also possible, right?
Anyhow, how can i make that the empty elements are represented as null
?
"null" simply means it doesn't point to any location. You can definitely use GameObject obj = null. You can also see it like GameObject "obj" is a basket and by default it doesnot contain anything which means null or empty. When you use something like obj = gameobjectAAA(suppose), it start to pointing gameobjectAAA,which means you have put gameobjectAAA in you empty basket(Gameobject "obj" ). Hope you understand.
It usually makes sense to catch null cases. The simplest way is to check with if statement before you perform your operation.
@stblue I know how Objects work and about their instances. Please read the question above. Because you say it is possible, though why doesn't it work in the case i am trying?
I simply check if null then i know it is empty.
I tested with
GameObject[,] grid;
grid = new GameObject[10, 5];
Debug.Log("1 Grid: " + grid);
grid[1, 2] = new GameObject();
Debug.Log("2 Grid: " + grid);
and it does not return null (both Debug.Logs). Are you sure Start()
is entered and the grid initialized before calling addBuilding
?
Your answer
Follow this Question
Related Questions
Initialising List array for use in a custom Editor 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Does Unity support application bootstrapping? 1 Answer
Is OnEnable fired right after Awake? 2 Answers