- Home /
How to randomize the order of objects?
Hi, so I have a situation set up where 10 different unique objects will enter from different directions and every time you press Spacebar, an object gets SetActive(false). My goal is to randomize the order successfully. And I have been able to randomize it. But the main problem is that even when lets say Object 5 gets SetActive(false), the randomization will still come back to trying to repeat Object 5 when it should not be selected anymore. Is there a better/simpler way to achieve this?
.
Any help would be appreciated!
.
public void RandomObjectFunction()
{
randObject = Random.Range (1, 10);
Objects [randObject].GetComponent<Animator> ().enabled = true;
MasterBool = true;
}
Answer by Priyanka-Rajwanshi · Apr 09, 2018 at 05:06 AM
@ILoveMyDada Try this:
int objectsDisabled = 0;
public void RandomObjectDisable()
{
if (objectsDisabled < Objects.Length)
{
int randObject = Random.Range(0, Objects.Length - objectsDisabled);
GameObject selectedObject = Objects[randObject];
Objects[randObject].SetActive(false);
for (int i = randObject; i < Objects.Length - 1; i++)
{
Objects[i] = Objects[i + 1];
}
Objects[Objects.Length - 1] = selectedObject;
objectsDisabled++;
}
}
Hey so this worked a bit. It's gotten as far as making sure no objects get repeated. Only problem was that some of the objects would just deactivate. But I believe I can work with this code. It's what I was trying to get to in terms of somehow $$anonymous$$using the objects that have been used already. Thank you!
Got it to work! I just changed Objects[randObject].SetActive(false) to Objects[randObject].GetComponent().enabled = true
Thanks so much!
Answer by Cornelis-de-Jager · Apr 09, 2018 at 03:04 AM
public void RandomObjectFunction()
{
// Make sure to run RandomObjectFunctionfunction only 10 times or it will hang
while (true) {
randObject = Random.Range (1, 10);
if (Objects [randObject].GetComponent<Animator> ().enabled == false) {
Objects [randObject].GetComponent<Animator> ().enabled = true;
break;
}
}
MasterBool = true;
}
This was close! It ends up crashing though. Is there a way around that? Thanks for the help!
Answer by Chimer0s · Apr 09, 2018 at 03:08 AM
You could reassign the position of the deactivated gameobject to be the last in the array of random objects, and instead of having Random.Range(1, 10) you could replace 10 with a variable int that is deducted from each time the function is called, effectively giving you a smaller pool that no longer includes the deactivated objects.
You could also create a list of game objects and add the deactivated ones to it as you go. Then in the randomization function check if the selected object is on that list, and if so, run the function again until one not on the list is selected.
Hey yeah this is something that I thought about, but I'm not that experienced enough to really come up with the code to implement it. Do you know how I could code this a bit with an example? Appreciate the response!
Answer by internationalfish · Apr 09, 2018 at 06:03 AM
Make sure you have a list of references that's only used for this purpose, shuffle the list once, and just pop from it when you want an object to deactivate. No worrying about tracking used indices; pop until the list is empty.
There's good discussion on C# options for randomizing a list in this StackOverflow thread (and lots of other places, I'm sure, since this may or may not be entirely up to date). I'm not suggesting a specific one myself because it's worth reading the thread regarding performance characteristics and differing implementations.
Hey thanks for the tip! Yeah I'm looking into using List ins$$anonymous$$d of Arrays since there is more functionality and freedom to mess with the objects under this setting. Cheers!
You're welcome. The same solution (Fisher-Yates shuffle) works with both arrays and lists, and honestly, this method would be much more efficient at solving your problem (and more readable/debuggable) than the method currently being used.
Answer by NonsenseSynapse · Apr 10, 2018 at 01:15 AM
One way of simplifying some of the already proposed answers is to use the C# LINQ tool. With it, you can do something like the following:
using System.Linq;
var filteredList = Objects.Where(obj => obj.isActive).ToList();
randObject = Random.Range (1, filteredList.Count);
filteredList[randObject].GetComponent<Animator>().enabled = true;
Here are a couple resources with more information about LINQ. It's a really neat tool for filtering and sorting data, definitely worth knowing about if you're developing in C#! https://www.foreach.be/blog/java-and-net-comparing-streams-linq https://unity3d.college/2017/07/01/linq-unity-developers/
Your answer

Follow this Question
Related Questions
Toggle 2D Object Active? 0 Answers
Spawning an object attached to one object, drawn to another. 1 Answer
Object positions in Scene view do not match Game view 1 Answer
different game "scenes" and optimization? 2 Answers
How do you make one object disappear and another appear with one button press? 1 Answer