Index out of range error
Here is my code
using UnityEngine; using System.Collections;
public class SpawnScript : MonoBehaviour {
public GameObject[] objects;
public float spawnMin = 1f;
public float spawnMax = 2f;
// Use this for initialization
void Start () {
Spawn();
}
void Spawn()
{
Instantiate(objects[Random.Range(0, objects.GetLength(0))],
new Vector3(transform.position.x + 20, transform.position.y, 0) ,
Quaternion.identity);
Invoke("Spawn", Random.Range(spawnMin, spawnMax));
}
}
but I am getting this error.
IndexOutOfRangeException: Array index is out of range. SpawnScript.Spawn () (at Assets/Script/SpawnScript.cs:19) SpawnScript.Start () (at Assets/Script/SpawnScript.cs:12)
any ideas to what I am doing wrong?
objects.GetLength(0)
probably isn't returning the length of the objects
array, wouldn't that be something like objects.Length
?
objects.GetLength(0)
This will return the size of the array. The last position will always be the size-1 (since the first position is 0). So try changing that to objects.GetLength(0)-1.
EDIT: Although, Random.Range(int,int) should be exclusive at the max int... (so you should get maximum objects.GetLength(0)-1 already).
You should check the length of your array before calling Spawn(). With no elements in the array (length = 0), Random.Range(0, 0) will return 0 and then you would get an IndexOutOfRangeException because objects[0] does not exist.
have you added the objects to this script in the inspector.
have you modfied the spawn$$anonymous$$in & spawn$$anonymous$$ax in the inspector .
I have added the objects (and the side) in the inspector, and I modified spawn$$anonymous$$in & spawn$$anonymous$$ax in the inspector aswell.
Are you sure you don't have any other scripts in the scene where you forgot to add objects to the array? The only reason your code as listed would fail, would be if the array was empty because accessing index 0 in an empty array would throw that error. In all other cases, Random.Range will work when used in this manner.
Answer by Statement · Mar 07, 2016 at 05:06 PM
Try adding a guard to see if your array is empty or not. If the message pops up in the console, single click the message to see which game object the script sits on, and ensure you got objects in the array.
using UnityEngine;
using System.Collections;
public class SpawnScript : MonoBehaviour {
public GameObject[] objects;
public float spawnMin = 1f;
public float spawnMax = 2f;
// Use this for initialization
void Start () {
Spawn();
}
void Spawn()
{
if (objects.GetLength(0) == 0)
{
print("No objects in array!");
Invoke("Spawn", Random.Range(spawnMin, spawnMax));
return;
}
Instantiate(objects[Random.Range(0, objects.GetLength(0))],
new Vector3(transform.position.x + 20, transform.position.y, 0) ,
Quaternion.identity);
Invoke("Spawn", Random.Range(spawnMin, spawnMax));
}
}
for some reason when i first tested his code in unity i got the same problem but now i re tested the same code & the problem is gone so i will delete my answer if it bathers you . by the way i have already asked him if the array is empty & he said now
have you added the objects to this script in the inspector.