- Home /
How to randomly spawn three non repeating gameobjects from an array?
Hi, I am pretty new to coding. So there probably is an easy answer for this problem. My game is a kids game were three shapes get spawned in randomly. I have figured out how to spawn them in and where, but I don't want any two shapes the same.
Here is my code`using UnityEngine; using UnityEngine.SceneManagement;
public class SpawnPickUps : MonoBehaviour{
public GameObject[] shapes;
public GameObject SpawnPoint01;// this is where I want the first random shape to spawn
public GameObject SpawnPoint02;
public GameObject SpawnPoint03;
void Start()
{
Instantiate(shapes[Random.Range(0, shapes.Length)], SpawnPoint01.transform);
Instantiate(shapes[Random.Range(0, shapes.Length)], SpawnPoint02.transform);
Instantiate(shapes[Random.Range(0, shapes.Length)], SpawnPoint03.transform);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
} ` Please let me know if I wasn't specific enough
and thank you!
Answer by BastianUrbach · Sep 05, 2019 at 09:58 PM
The other answer probably works but imo this little helper function is a bit more elegant:
T[] Shuffle<T>(T[] array) {
array = (T[])array.Clone();
for (int i = 0; i < array.Length; i++) {
var random = Random.Range(i, array.Length);
var temp = array[i];
array[i] = array[random];
array[random] = temp;
}
return array;
}
It takes an array and returns an array with the same elements in random order.
You could for example use it like this:
var shuffledShapes = Shuffle(shapes);
Instantiate(shuffledShapes[0], SpawnPoint01.transform);
Instantiate(shuffledShapes[1], SpawnPoint02.transform);
Instantiate(shuffledShapes[2], SpawnPoint03.transform);
Answer by giuse035 · Sep 05, 2019 at 07:57 PM
You can use the RandomFromArray function to extract a random element from an array of any type (gameobject, string, etc...)
The function is the used inside the ThreeRandomFromArray function which inputs an array and outputs three elements of the same type of the list (string, int, etc...) .
//This function can be used for an array of any type. to use lists, use
//TheListYouWantToUse.ToArray ()
//all 3 output values must be of the same type of the list (can only out strings from a string list)
private void ThreeRandomFromArray<T> (T[] array, out T out0, out T out1, out T out2) {
//This is important because if the array is less than 3 items long it will get stuck and freeze unity, as it
//would try to extract three different items from an array long only 2 or 1 items.
//exceptions also stop the execution of the function and all of the calling functions.
if (array.Length < 3)
throw new System.Exception ("Trying to randomly extract 3 random elements from an array" +
"that is only " + array.Length + ". Aborting.");
//cache all values
T cached0 = RandomFromArray (array);
T cached1;
T cached2;
GetSecond: cached1 = RandomFromArray (array);
//"GetThird" allows to "rewind" and execute another time the code from here
if (cached1.Equals (cached0)) //if equal go back to "GetSecond"
goto GetSecond; //go back to GetSecond
//"GetThird" allows to "rewind" and execute another time the code from here
GetThird: cached2 = RandomFromArray (array);
if (cached2.Equals (cached0) || cached2.Equals (cached1)) //if equal go back to "GetThird"
goto GetThird; //go back to GetThird
out0 = cached0;
out1 = cached1;
out2 = cached2;
}
//This function can be used with an array of any type.
private T RandomFromArray<T> (T[] list) {
return list[Random.Range (0, list.Length)];
}
And then change the code inside the start function to:
void Start () {
//Prepare the gameobject varaibles that will be written
GameObject object0;
GameObject object1;
GameObject object2;
//Run the function and actually assign the previously declared gameobjects
ThreeRandomFromArray (shapes, out object0, out object1, out object2);
//Create object0, object1, object2 at thei corresponding spawn position
Instantiate (object0, SpawnPoint01.transform);
Instantiate (object1, SpawnPoint02.transform);
Instantiate (object2, SpawnPoint03.transform);
}
EDIT: Also check coeusueoc' s answer (below), since it is more versatile and cleaner overall.
Your answer
Follow this Question
Related Questions
What is the best way to instatiate an array of connected GameObjects? 0 Answers
C# GameObjects Instantiate Into Each Other Issue 1 Answer
C# 2d Instantiate object randomly around gameobject 1 Answer
Want replace a current object with one inside array the object being replaced with is prefab 1 Answer
Randomly instantiate objects from array without choosing the same item twice. 2 Answers