- Home /
 
The question is answered, right answer was accepted
Index out of range when trying to spawn objects using spawn location list
Hello everyone, I'm new to Unity and C# I require a detailed help.
I'm currently creating my first self project where 3 platforms that the player shouldn't go and 1 platform that s/he should. These platforms are created around the player on specific positions (up, down, left, right). So I try to spawn these platforms on 4 random locations using a spawner location list. My aim is to create 3 false platforms on random locations without overlapping each other, and creating 1 true platform overlapping no other platform. My code to spawn platforms are in Spawner() function. But if I try to run my game, i get an error saying my randomSpawnerIndex is out of range on falsePlatform2:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class GameManager : MonoBehaviour
 {
     public PlayerController movement;
 
     public Transform player;
     public Transform playerPlatform;
     public Transform cameraPosition;
 
     public GameObject truePlatform;
     public GameObject falsePlatform;
     public GameObject Spawn1;
     public GameObject Spawn2;
     public GameObject Spawn3;
     public GameObject Spawn4;
 
     private Vector3 platformYOffset;
     private Vector3 cameraYOffset;
 
     bool gameHasEnded = false;
     public float restartDelay = 1f;
 
     void Start()
     {
         Spawner();
     }
 
 
     
 
     public void EndGame()
     {
         if(gameHasEnded == false)
         {
             gameHasEnded = true;
             Invoke("Restart", restartDelay);
         }
     }
 
     
     //RESTART
     
     void Restart()
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
     }
 
 
     
 
 
     public void OnTrueMove()
     {
         platformYOffset = new Vector3(0, (float)0.5, 0);
         GameObject[] falsePlatforms = GameObject.FindGameObjectsWithTag("FalsePlatform");
         foreach (GameObject falseOne in falsePlatforms)
         {
             Destroy(falseOne);
         }
 
         truePlatform.GetComponent<Transform>().position = new Vector3(0, -3, 0);
 
         platformYOffset = new Vector3(0, (float)0.5, 0);
         cameraYOffset = new Vector3(0, 8, 0);
 
         playerPlatform.position = player.position - platformYOffset;
         cameraPosition.position = player.position + cameraYOffset;
         Spawner();
     }
 
     
     void Spawner()
     {
         List<Vector3> spawnLocs = new List<Vector3>();
         spawnLocs.Add(Spawn1.transform.position);
         spawnLocs.Add(Spawn2.transform.position);
         spawnLocs.Add(Spawn3.transform.position);
         spawnLocs.Add(Spawn4.transform.position);
 
         Debug.Log(spawnLocs.Count);
 
         platformYOffset = new Vector3(0, (float)0.5, 0);
 
         int randomSpawnerIndex = Random.Range(0, spawnLocs.Count);
 
         GameObject instantiatedFalse1 = Instantiate(falsePlatform, spawnLocs[randomSpawnerIndex] - platformYOffset, Quaternion.identity);
         spawnLocs.RemoveAt(randomSpawnerIndex);
         Debug.Log(spawnLocs.Count);
         GameObject instantiatedFalse2 = Instantiate(falsePlatform, spawnLocs[randomSpawnerIndex] - platformYOffset, Quaternion.identity);
         spawnLocs.RemoveAt(randomSpawnerIndex);
         Debug.Log(spawnLocs.Count);
         GameObject instantiatedFalse3 = Instantiate(falsePlatform, spawnLocs[randomSpawnerIndex] - platformYOffset, Quaternion.identity);
         spawnLocs.RemoveAt(randomSpawnerIndex);
         Debug.Log(spawnLocs.Count);
 
         GameObject instantiatedTrue = Instantiate(truePlatform, spawnLocs[randomSpawnerIndex] - platformYOffset, Quaternion.identity);
         spawnLocs.RemoveAt(randomSpawnerIndex);
     }
 }
 
 
               I tried to create the true platform using the last element of the spawnLocs list.
I look forward to your help and any suggestions that make my very basic game mechanic become real.
I forgot to mention that the code works without any problem only on the below placements. But other than that; it creates 1, 2 or 3 red platforms but no green platform.

Answer by Hellium · Oct 20, 2020 at 09:48 AM
You need to update randomSpawnerIndex after removing elements from the spawnLocs list.
I highly advise you to put the spawn logic in a function
 void Spawner()
 {
     List<Vector3> spawnLocs = new List<Vector3>();
     spawnLocs.Add(Spawn1.transform.position);
     spawnLocs.Add(Spawn2.transform.position);
     spawnLocs.Add(Spawn3.transform.position);
     spawnLocs.Add(Spawn4.transform.position);
     platformYOffset = new Vector3(0, 0.5f, 0);
     SpawnPlatform(spawnLocs, falsePlatform, platformYOffset);
     SpawnPlatform(spawnLocs, falsePlatform, platformYOffset);
     SpawnPlatform(spawnLocs, falsePlatform, platformYOffset);
     SpawnPlatform(spawnLocs, truePlatform, platformYOffset);
 }
 
 void SpawnPlatform(List<Vector3> spawnLocs, GameObject prefab, Vector3 offset)
 {
     if(spawnLocs.Count == 0)
     {
         Debug.LogError("No spawn location available");
         return;
     }
     int randomSpawnerIndex = Random.Range(0, spawnLocs.Count);
     GameObject instance = Instantiate(prefab, spawnLocs[randomSpawnerIndex] - offset, Quaternion.identity);
     spawnLocs.RemoveAt(randomSpawnerIndex);
 }
 
              Thanks, much appreciated. The code works without an error and the game continues as I make the right moves.