- Home /
IndexOutOfRangeException for initial spawn of prefabs from an array
I've been following along with a tutorial on Infinite Runners found here:
https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/infinite-runner
I'm doing a slightly modified version for my own project. Everything seems to work fine, except the game gives me an IndexOutOfRangeException immediately after I press the play button. Afterwards, the Spawn function seems to have no problems. Here's my code:
using UnityEngine;
using System.Collections;
public class SpawnScript : MonoBehaviour {
public GameObject[] obj;
public float spawnMin = 3f;
public float spawnMax = 4f;
// Use this for initialization
void Start () {
Spawn ();
}
void Spawn () {
int random = Random.Range(0, obj.Length);
Debug.Log("Random array number: " + random + " GetLength: " + obj.GetLength(0) + " Length: " + obj.Length);
Instantiate(obj[random], transform.position, Quaternion.identity);
Invoke ("Spawn", Random.Range(spawnMin, spawnMax));
}
}
Here is a screencap of the quad this script is attached to:
(The quad is a child of the Main Camera object)
As you can see I added a logging statement, here's the console output, including the logging statement and error:
Random array number: 1 GetLength: 2 Length: 2
UnityEngine.Debug:Log(Object)
SpawnScript:Spawn() (at Assets/Scripts/SpawnScript.cs:29)
SpawnScript:Start() (atAssets/Scripts/SpawnScript.cs:14)
Random array number: 0 GetLength: 0 Length: 0
UnityEngine.Debug:Log(Object) SpawnScript:Spawn() (at Assets/Scripts/SpawnScript.cs:29)
SpawnScript:Start() (at Assets/Scripts/SpawnScript.cs:14)
IndexOutOfRangeException: Array index is out of range.
SpawnScript.Spawn () (at Assets/Scripts/SpawnScript.cs:30)
SpawnScript.Start () (at Assets/Scripts/SpawnScript.cs:14)
Random array number: 1 GetLength: 2 Length: 2
UnityEngine.Debug:Log(Object) SpawnScript:Spawn() (at Assets/Scripts/SpawnScript.cs:29)
Strangely enough, it seems that Start function calls Spawn() twice. The first time it shows the correct length of the array, but the second it shows an empty array. Any idea why this is happening?
Answer by Cherno · Sep 28, 2015 at 09:00 PM
You use GetLength with a one-dimensional array. GetLength is only used for multi-dimensional arrays where GetLength(3] would return the Length of the fourth dimension of the array, for example. For one-dimensional arrays, only use Length.
Maybe you have a second Spawn component on some GameObject in the scene? Also, it would help to know which exact line of code the error occurs in, since the message your posted says line 30 which is beyond the code you posted.
Ok, I would've just used Length but for some reason the guy in the tutorial used GetLength and seemed to say that using Length was incorrect.
You were right. Somehow the SpawnScript was also on the $$anonymous$$ain Camera object. Thanks for your help!
Your answer
Follow this Question
Related Questions
For loop not running correctly inside start () 1 Answer
How can I get every other GameObject except for the one I'm using? 1 Answer
Invoke fails: "Trying to Invoke method: InputController.enableInput couldn't be called." 1 Answer
Initialising List array for use in a custom Editor 1 Answer
Invoke not working from Start() ? 1 Answer