I keep on getting this error whenever my code is run. The code is meant to generate a random index within an array to select a prefab from a list defined by a certain tag (B). The issue is that the unity console is saying that the index given is outside of the range inside the array, which it shouldn’t be. I have no idea why this is happening, and Debug.Log has been little help. Any assistance would be greatly appreciated!
private void Start()
{
Invoke("Spawn", 0.1f);
Invoke("Array", 0.05f);
}
public void Array()
{
TopObjects = GameObject.FindGameObjectsWithTag("T");
BottomObjects = GameObject.FindGameObjectsWithTag("B");
LeftObjects = GameObject.FindGameObjectsWithTag("L");
RightObjects = GameObject.FindGameObjectsWithTag("R");
}
private void Spawn()
{
if (spawned == false)
{
if (openingDirection == 1)
{
// bottom room spawn
rand = Random.Range(0, BottomObjects.Length);
BottomObjects[rand] = BottomObject;
BottomObject.SetActive(true);
BottomObject.transform.position = transform.position;
BottomObject.transform.rotation = Quaternion.identity;
Debug.Log(rand);
}
}
}