Hi, does anybody else get null reference errors for things that should already be instantiated? I’ve run into this twice now.
//\Scripts\Game_Scene\GameManager
void Start () {
...
Instantiate(boardManager);
...
//startPosition = new Vector3((BoardManager.instance.maxCols / 2), (BoardManager.instance.maxRows / 2), 0f);
Invoke("Test", 0f);
}
private void Test()
{
startPosition = new Vector3((BoardManager.instance.maxCols / 2), (BoardManager.instance.maxRows / 2), 0f);
}
So game manager starts up on a scene, and it instantiates one prefab BoardManager gameobject and its singleton script object. It has two public variables, maxRows and maxCols that I want to use to setup a variable in this game manager script, so I’d assume those maxRows and maxCols should be available as soon as I it finishes instantiating BoardManager, but instead, I get a null reference error from anything in BoardManager.
Testing it, if I invoke a method “Test” with a delay of 0f seconds, it picks up BoardManager vars no problem. Is there a delay or some kind of method the compiler takes that would cause this issue with instantiate? From what I could find, it looks like instantiate is supposed to be straightforward, ie no delay or short cutting around code. Any help? Or should I always just have some sort of second “Start” method that is called when everything has finished loading?
So, with the sample code above, if I uncomment startPosition’s assignment before the invoke, I get an null reference exception thrown at line 10. In boardmanager, I don’t use any invoke, coroutines, or anything I think is supposed to have a delay to it. My full code can be found:
Thanks!