Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Issues with Lists and .Remove

Discussion in 'Scripting' started by thegreatkoala, Apr 12, 2020.

  1. thegreatkoala

    thegreatkoala

    Joined:
    Jun 30, 2017
    Posts:
    11
    Below is my list for spawning a group of objects. These are cube, cylinder, capsule, sphere and a copy of each to create 8 objects. There are 8 spawn points set in the inspector. My aim is to create a list of the 8 game objects, pick one at random, spawn it in spawn point 0

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SpawnController : MonoBehaviour
    6. {
    7.     public GameObject[] spawnArray; //array of spawn points
    8.  
    9.     public GameObject[] myArray;
    10.     public List<GameObject> myList;
    11.     //public HashSet<GameObject> myList;  
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {      
    16.         myArray = Resources.LoadAll<GameObject>("Level1/"); //array of game objects in Resources/Level1
    17.         myList = new List<GameObject>(myArray); //creates a list based on the above array
    18.        
    19.         LoadSpawnPoints();
    20.         SpawnObjects();
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.         Debug.Log(myArray.Length);
    27.         Debug.Log(myList.Count);
    28.     }
    29.  
    30.     void LoadSpawnPoints() //loads the spawn points based on the spawn points array
    31.     {
    32.         for (int i = 0; i < spawnArray.Length; i++)
    33.         {
    34.             Instantiate(spawnArray[i], spawnArray[i].transform.position, spawnArray[i].transform.rotation);          
    35.         }      
    36.     }
    37.  
    38.     void SpawnObjects() //spawns the objects in the myList list and places them according to the spawn points
    39.     {
    40.         for (int i = 0; i < myList.Count; i++)
    41.         {
    42.             int ind = Random.Range(0, myList.Count); //randomly selects an object from myList
    43.             Instantiate(myList[ind], spawnArray[i].transform.position, spawnArray[i].transform.rotation);
    44.             myList.Remove(ind);
    45.         }
    46.     }
    47. }
    48.  
    The line myList.Remove(ind) at the very bottom comes up with the below error:
    upload_2020-4-12_14-11-41.png
    How do I fix said error?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    You want "myList.RemoveAt(ind)".

    However there's an issue with that for loop. Removing elements from the list while iterating through it may give you unexpected results.
     
    thegreatkoala likes this.
  3. thegreatkoala

    thegreatkoala

    Joined:
    Jun 30, 2017
    Posts:
    11
    Thank you PraetorBlue. I've done as you suggested and that did what I wanted. Regarding the issue you mentioned and "unexpected results", you were right. Changing
    Code (CSharp):
    1. i < myList.Count
    to
    Code (CSharp):
    1. i < myArray.Length
    I believe fixed that.

    Again, thank you
     
  4. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    When you remove stuff from a list you should iterate backwards. This way only indices are shifted which have already been processed.
     
    thegreatkoala likes this.