NullReferenceException: Object reference not set to an instance of an object (int[,])
Hello, I am making simple game where I need to use two dimensional array, so I am using int[,]
.
Then on the scene load I am assigning values like this:
//GameBoardController.js
function getMatrix(){
for (var i = 0; i < numrows; ++i){
var columns: Array = [];
for (var j = 0; j < numcols; ++j){
activeCells[i,j] = 0;
}
}
}
}
When I print print(activeCells)
now, i get System.Int32[,]
which is expected result.
But then in another function which is triggered by button:
//GameBoardController.js
function resetScene(){
for(var x = 0 ; x < sizeX ; x++){ //sizeX is as public int variable
for(var y = 0 ; y < sizeY ; y++){ //sizeY is as public int variable
activeCells[x,y] = 0;
}
}
}
And here is the problem. Function resetScene()
is failing and giving me error message:
NullReferenceException: Object reference not set to an instance of an object
GameBoardController.resetScene () (at Assets/Scripts/GameBoardController.js:834)
But this is wrong because in the same script few lines above resetScene()
I am assigning and then printing activeCells
and everything is working.
Does please anyone know what am I doing wrong? Should I attach another GameBoardController
script to the button? GameBoardController
is attached to empty GameObject
and everything but this works.
Answer by vincurekf · Dec 17, 2016 at 04:00 PM
Nevermind, I had old controller (old GameObject with GameBoardController deactivated) assigned in the button. What a mystery :/
Your answer
![](https://koobas.hobune.stream/wayback/20220612102921im_/https://answers.unity.com/themes/thub/images/avi.jpg)