- Home /
Randomly remove list values
I got a platform with 32 obstacles which are all included into an array (by tag). I transfer all of these values to a list using .ToList.
Everything works fine and all of the values are transferred to the list. Once the scene is played all of the 32 platforms are spawned which is also good.
What I want to do is to randomly remove values from the list and then transfer them back to the array (I know how to transfer them but not how to randomly remove the values). I tried with RemoveRange but this only removes values from a starting value to an end value even if randomized with Random.Range.
I want to randomly remove multiple items from the list not in order (for example remove the first item on the list then the 3rd then the 5th etc...).
I could do that by writing list.Remove(list[Random.Range(1,32)]); but I hope there are better methods..
Below is my code :
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class RandomizeSpawn : MonoBehaviour {
public GameObject wallObs;
private GameObject[] allspawnPoint;
private GameObject[] spawnPoint;
private List<GameObject> disabledSpawns = new List<GameObject>();
private bool callOnce;
void Start () {
wallObs = GameObject.Find ("Wall");
callOnce = true;
}
void SpawnObstable () //Where the obstacles are spawned
{
foreach (GameObject respawnPoint in spawnPoint)
{
Instantiate(wallObs, new Vector3 (respawnPoint.transform.position.x,wallObs.transform.position.y,respawnPoint.transform.position.z), respawnPoint.transform.rotation);
callOnce = false;
}
}
void Update () {
allspawnPoint = GameObject.FindGameObjectsWithTag ("Spawner");
if (callOnce == true) {
disabledSpawns = allspawnPoint.ToList ();
Debug.Log (disabledSpawns.Count);
RandomizeSpawns ();
}
}
void RandomizeSpawns () //Where I remove only 1 item using random.range
{
disabledSpawns.Remove(disabledSpawns[Random.Range(1,32)]);
spawnPoint = disabledSpawns.ToArray (); //this is where I transfer the objects from the list to the array to get spawned
SpawnObstable ();
}
}
Convert the list to array and use random function with array length-1 as limit
Answer by Kishotta · Jan 18, 2018 at 08:24 AM
You could do:
disabledSpawns.RemoveAt(Random.Range(0, disabledSpawns.Count - 1));
Only removes 1 object. I want multiple objects (for example 10) to be removed from the list (which is 32 objects long) at once
Well, you can just use a for loop then: for(int x = 0; x < 10; x++){ [Code to remove random spawnpoint here] }
Thanks to all of you , this works
for (int x = 0; x < 12; x++) {
disabledSpawns.RemoveAt(Random.Range(0, disabledSpawns.Count - 1));
Debug.Log (disabledSpawns.Count);
}
Watch out, it should use Random.Range(0, disabledSpawns.Count)
, because Random.Range(int $$anonymous$$, int max)
is inclusive-exclusive (the float version is inclusive-inclusive). If you leave it at disabledSpawns.Count - 1
it will never try to remove the last element!
Your answer
Follow this Question
Related Questions
Pick between two floats 2 Answers
A node in a childnode? 1 Answer
Random select from array and spawn 1 Answer
Selection list from Array Unity - Random - GameObjects array 1 Answer
Creating a record of values generated with Random.Range 1 Answer