Instantiating a prefab and storing it in a multi-dimensional array

I have a script called InitGameBoard. I have a multi-dimensional array of game objects declared at the beginning like this:

public GameObject[,] floorContents=new GameObject[20,20]; 

I am trying to store an Instantiated prefab in the multi-dimensional array like this:

Vector3 select2ndPos=new Vector3(dieEight,0,dieSix);
floorContents[dieEight,dieSix]=Instantiate(fireObject,select2ndPos,transform.rotation)as GameObject;

and then I am trying to destroy it and remove it from the scene like this:


But it doesn’t seem like it is even saving the object in the array, and it isn’t giving me any errors. Any idea what I’m doing wrong?

Make sure the type of fireObject is GameObject.

If the type is a script class, then use fireObject.gameObject in Instantiate method.

Btw, I don’t prefer to use “as” for casting purposes. Because it doesn’t throw an exception when there is a type casting mismatch. It just sets it to null silently.

To be able to catch that casting errors, you should use regular casting like…