- Home /
IndexOutOfRangeException: Array index is out of range
IndexOutOfRangeException: Array index is out of range, it's my problem with this script at 37th line (GameObject Newaster = Instantiate (aster[spawnObject], spawnPoints [spawnIndex].position, spawnPoints [spawnIndex].rotation) as GameObject;):
using System.Collections; using UnityEngine; using UnityEngine.Networking; using System.Collections.Generic;
public class Asteroid : NetworkBehaviour {
public float timeTospawn = 0.5f;
public Transform[] spawnPoints;
public GameObject[] aster;
public List<Transform> possibleSpawns = new List<Transform>();
// Use this for initialization
void Start()
{
//fill possible spawn
for (int i = 0;i<spawnPoints.Length;i++)
{
possibleSpawns.Add(spawnPoints[i]);
}
InvokeRepeating("SpawnItems", timeTospawn, timeTospawn);
}
// Update is called once per frame
void Update () {
}
void SpawnItems()
{
int spawnIndex = Random.Range(0, possibleSpawns.Count);
int spawnObject = Random.Range(0, aster.Length);
GameObject Newaster = Instantiate (aster[spawnObject], spawnPoints [spawnIndex].position, spawnPoints [spawnIndex].rotation) as GameObject;
Newaster.GetComponent<DestroyAsteroid>().mySpawnPoint = possibleSpawns[spawnIndex];
possibleSpawns.RemoveAt(spawnIndex);
}
}
So the issue is that the array element you're trying to access doesn't exist.
One possible issue: Does it happen time its called or does it work sometimes? Random.Range is inclusive which means it can return the highest possible value. Say your array has 10 elements, so aster.Length = 10. that means spawnObject has a chance to equal 10. but the 10th element in the array is element 9, because it counts from 0, not 1. I'd try:
int spawnObject = Random.Range(0, aster.Length - 1);
The list may have a similar issue.
Alternatively:
You're using possibleSpawns.Count to set spawnIndex, and then using spawnIndex to access an element from spawnPoints.
Is spawnPoints definitely longer than possibleSpawns?
I'd be debug.logging all the array lengths and index values and making sure they are within each others ranges. Sorry I can't help more but it kinda depends on what's in your public arrays
Thank's to reply, but it don't works, now it's this line who doesnt works with the same error : GameObject Newaster = Instantiate (aster[spawnObject], spawnPoints [spawnIndex].position, spawnPoints [spawnIndex].rotation) as GameObject;
Can you send me all the line changed
Because I'm not English and I didn't understand all you said
There is nothing necessarily wrong with than line. the issue may be the length of your public arrays. I suggest debug.logging out the length of those arrays and the indexes to make sure they are correct before the line it errors on.
keeping in $$anonymous$$d that the 10th element in an array is element 9 (it will always be one less)
Answer by AgarFun · Jul 03, 2017 at 05:56 AM
Thank's to reply, but it don't works, now it's this line who doesnt works with the same error : GameObject Newaster = Instantiate (aster[spawnObject], spawnPoints [spawnIndex].position, spawnPoints [spawnIndex].rotation) as GameObject;
Can you send me all the line changed
Your answer
Follow this Question
Related Questions
Ontriggerenter and InvokeRepeating problem 1 Answer
How To Random Range a Float? 2 Answers
How to spawn prefab from a single cache when using multiple caches? 0 Answers
I need to Stop and Restart a Co-Routine at anytime 1 Answer
Spawn Controller... 2 Answers