I have a bug in my code and I could not find it. I think my Bug is in the declaration.
Why is this not working:
public class Test : MonoBehaviour
{
public int Biom = 1;
public int Type = 0;
}
public class ApplyBioms : MonoBehaviour {
private static int Size = BlockGen.Size;
public Test[,,] stats = new Test[Size, Size, Size];
private int counter = 0;
if(k-1>=0)
{
if (stats[k - 1, i, j].Biom == 2)
{ counter++; }
}
Error:
NullReferenceException: Object reference not set to an instance of an object.
Thanks for the Help.
~Speedy
You should not create mono behaviours with the new keyword.
In addition to that you did not create entries in the array, you only created (or would have) created an array of sufficient size, but with all null (default) values.
1 Like
True, but to be clear, he has not created any MonoBehaviours with the new keyword. He created an array with the new keyword, which is perfectly fine (and the only way to do it).
But yes, you create this array of null references on line 10, and then promptly (on line 15) start trying to dereference them. Obviously the runtime has no choice at that point but to raise a NullReferenceException.
(EDIT: using line numbers in the code shown, which of course can’t be the actual code, as it wouldn’t compile… you can’t have executable code just hanging out in a class definition like that, outside of any method.)
Thanks for the correction on the array.