IndexOutOfRangeException: Array index is out of range / EnemiesSpawner.cs:18)
I am getting this error IndexOutOfRangeException: Array index is out of range. EnemiesSpawner.Spawn () (at Assets/EnemiesSpawner.cs:18)
It keeps popping up . When do I spawn my enemies they spawn out fine to me . Here is my script :
using UnityEngine;
using System.Collections;
public class EnemiesSpawner : MonoBehaviour {
public GameObject enemy;
public Transform [] spawnPoints;
public float spawnTime = 5f;
void Start () {
InvokeRepeating("Spawn", spawnTime, spawnTime); //Calls the "Spawn" function every 10 seconds.
}
void Spawn () {
int spawnPointIndex = Random.Range (0, spawnPoints.Length); for( int spawnCount = 20 ; spawnCount > 0 ; --spawnCount )
Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
}
}
Answer by nate-sewell · Aug 17, 2016 at 03:48 AM
personally I'd use: int spawnCount = spawnPoints.Length - 1
rather than: int spawnCount = 20
also, make sure that spawnPoints has actually been initialized AND populated based on the fact that it's public in a MonoBehaviour, it is /possible/ that it's initialized in the editor...
also spawnCount > 0 means the initial entry will never be used
When I deleted the old code and add the int spawnCount = spawnPoints.Length - 1 it wouldn't spawn any enemies. I trying to spawn to different enemies and one spawn the other one didn't why is that ?
if you have 2 you want to use the indices 0, 1
using spawnPoints.Length - 1 gets you the correct top number
using spawnCount > 0 prevents you from entering the loop when you get down to 0
to get the full range range use this as the loop condition spawnCount >= 0
What do mean indices 0,1 . ? I am not understanding you . What would be an example of 20 or less in code .
The is what I have for for now :
using UnityEngine;
using System.Collections;
public class EnemiesSpawner : $$anonymous$$onoBehaviour {
public GameObject enemy;
public Transform [] spawnPoints;
public float spawnTime = 5f;
void Start () {
InvokeRepeating("Spawn", spawnTime, spawnTime); //Calls the "Spawn" function every 10 seconds.
}
void Spawn () {
int spawnPointIndex = Random.Range (0, spawnPoints.Length); for( int spawnCount = spawnPoints.Length - 1 ; spawnCount >= 0 ; --spawnCount )
Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
}
}
I think perhaps the other poster had a more accurate answer than the one I gave, for some reason I thought you were using the spawnCount variable inside the for loop's body as well, but it lloks like you used spawnPointIndex ins$$anonymous$$d.... anyway, it should look something like this
int spawnPointIndex = Random.Range (0, spawnPoints.Length -1); for( int spawnCount = 20 - 1 ; spawnCount >= 0 ; --spawnCount ) Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
can you screen cap the portion of the editor where you assigned values into the SpawnPoints array and attach it in a response?
yup, the section where you attached the script to something in the scene or a prefab
Answer by Cherno · Aug 17, 2016 at 03:43 AM
Using Random.Range with ints makes the max value be inclusive; that means that if you have an array length of 3 (elements at index 0,1,2) then with your code you get a value of 0,1,2 or 3. Since 3 is higher than the highest index value (2) you get the out of range error.
Solution: Use spawnPoints.Length - 1 for the max value.
This is incorrect. "Note that max is exclusive, so using Random.Range( 0, 10 ) will return values between 0 and 9" - https://docs.unity3d.com/ScriptReference/Random.Range.html
Answer by tanoshimi · Aug 17, 2016 at 06:16 AM
From the look of your screenshot, the object which you're spawning is the same as the object to which this script is attached? That sounds wrong, and will lead to an exponential growth in the number of spawners (the new clones of which won't have any spawnobjects assigned).... I would suggest that you have a single spawner manager object to wbich this script is attached, but it should not also be attached to individual spawnpoints.
If that does not resolve your index out of range, make liberal use of Debug.Logs to print values of each of your variables.
Okay I want to spawn in different location in scene .
I thought that when I saw the screenshot, but it is actually a spawnPoint that is self referential, not the item being spawned I recreated the scenario as best I could but ti works fine for me.
if you are wanting to spawn two different prefabs at the same spot...
Reuse the script and for enemy use a different object from the scene or prefab, and fill in the spawn points the same way you have it now
Your answer
Follow this Question
Related Questions
Array Index Is Out Of Range... 1 Answer
,"For" loop looping only once through my array 0 Answers
Unknown Argument Out of Range Index Error On Card Game 1 Answer
index out of range problem with for loops and arrays. 1 Answer
Reference all objects in an Array except for the object the script is on. 1 Answer