- Home /
Create random order and go through till the end (C#)
I have a list of gameobjects (prefabs) that I want to randomly instantiate in order. I used GameObject[] to create the list and have specified the prefabs in the inspector. To obtain the next prefab to spawn I currently pick one at random from the list, but since I only want the items to occur once that isn't really helping.
I am still a beginner at C#, so I would like to have an example of how to do this with an easy way to adapt to possible future iterations.
Any help is always welcome!
Answer by Xarbrough · Oct 15, 2015 at 12:24 PM
Here is some simple code in the Start() method and some more ideas in some methods below. Save those other approaches for later, if they are confusing at this point.
One simple approach:
Make a copy of the prefabs array. Use a list for easy resizing.
As long as there are elements inside.
Pick a random index from the list.
Instantiate the random element.
Remove it from the list, so it won't be picked again.
Repeat (2).
using UnityEngine; using System.Collections.Generic;
public class Spawner : MonoBehaviour { public GameObject[] prefabs;
private void Start () { // Only try to instantiate, if prefab array was actually populated through the inspector. // This should always be the case, but maybe somebody does funky stuff with this script, so better check. if(prefabs != null) { // Make a copy of the prefab array. A System.Collections.Generic List is easily resizable. List<GameObject> spawnList = new List<GameObject>(prefabs); // As long as there are elements in the collection. while(spawnList.Count > 0) { // Pick a random index from the available options. int randomIndex = Random.Range(0, spawnList.Count); // If the element at the picked index exists, instantiate it. if(spawnList[randomIndex] != null) { GameObject instance = Instantiate(spawnList[randomIndex]); Debug.Log ("Created: " + instance.name + " " + instance.GetInstanceID()); } // In any case, remove the random element from the list. // This way it won't be instantiated again. spawnList.RemoveAt(randomIndex); } } } /// <summary> /// Instantiates any object collection of prefabs in random order. /// </summary> private void InstantiateInRandomOrder(IEnumerable<Object> prefabs) { if(prefabs == null) throw new System.ArgumentNullException("prefabs", "Prefab collection cannot be null."); var spawnList = new List<Object>(prefabs); while(spawnList.Count > 0) { int randomIndex = Random.Range(0, spawnList.Count); if(spawnList[randomIndex] != null) { Object instance = Instantiate(spawnList[randomIndex]); Debug.Log ("Created: " + instance.name + " " + instance.GetInstanceID()); } spawnList.RemoveAt(randomIndex); } } /// <summary> /// Instantiates a generic collection of prefabs in random order and performs some action on each. /// </summary> private void InstantiateInRandomOrder<T>(IEnumerable<T> prefabs, System.Action<T> callback) where T : UnityEngine.Object { if(prefabs == null) throw new System.ArgumentNullException("prefabs", "Prefab collection cannot be null."); var spawnList = new List<T>(prefabs); while(spawnList.Count > 0) { int randomIndex = Random.Range(0, spawnList.Count); if(spawnList[randomIndex] != null) { T instance = Instantiate(spawnList[randomIndex]); callback(instance); } spawnList.RemoveAt(randomIndex); } } }
Your answer
Follow this Question
Related Questions
Instantiate problem with selected objects 0 Answers
Why is my Prefab Instantiating when the Scene is Loaded? 2 Answers
Removing instantiated objects?? 1 Answer
Problem with Line Renderer 1 Answer
Multiple Cars not working 1 Answer