Hi there, this is probably a simple fix but I can’t find a solution
I’m just trying to instantiate a gameobject multiple times into a grid shape using two for loops. When I try to add the instantiated object to a 2d array however, an error is thrown saying the object reference is not set to an instance of an object, despite my Debug.Log(object) clearly displaying the object is there?
public GameObject prefab;
pubic Collider2D[,] Colliders;
public int gridWidth = 6;
public int Gridheight = 8;
void Start()
{
for (int x = 0; x < gridWidth; x++)
{
for (int y = 0; y < gridHeight; y++)
{
Collider2D newCollider = Instantiate(prefab, transform.position + new Vector3(x, y, 0), transform.rotation).GetComponent<Collider2D>();
Debug.Log(newCollider);
Colliders[x, y] = (BoxCollider2D)newCollider;
}
}
Debug.Log(Colliders);
}
Any help would be massively appreciated, and any extra info you need I can happily provide. I’ll be active for the next few hours
This was perfect and helped solve my issue, thank you so much.
For any future users, it turns out in my 2D Array definiton I hadn’t defined the dimensions of the array and thus it was just empty, and so when I tried to assign the instantiated objects to a null array things went boom. Thank you again ^u^