I keep getting a “Object reference not set to an instance of an object” error every time I try to define the size of an array, which is located in an array of class objects. Here is an simplified, detached example of what I am trying to accomplish:
var matrixItem : mClass[];
class mClass{
obj : Vector2[];
}
function Awake(){
matrixItem = new mClass[10];
for(i = 0; i < matrixItem.length; i++){
matrixItem*.obj = new Vector2[64];*
}
}
Can anyone help me determine what I am doing wrong? Thank you for any advice!
Hello,
Your problem is not the creation of the built-in array. It would have been a more simple thing to solve had you posted the actual error (including line number) rather than only quote it.
When you create a new built-in array of non-struct objects, then each element is always going to be null
. So when you create a new mClass[2]
you will get something line [null, null]
. A null
value does not have a obj
field and therefore throws an error.
When using built-in arrays, you need to initialize the elements so that they are not null
:
for (i = 0; i < array.length; i++) {
array *= new mClass();*
array*.obj = new Vector2[64];*
}
For a Vector2
array however, you do not need to do this because Vector2
is a structure not a class.
Hope this helps,
Benproductions1