- Home /
Foreach Statement Random Sequence/Order?
How do I randomize the sequence of items in a foreach statement?
Foreach iterates all the items inside a transform in an order. Is there a way I can randomize that order?
For example:
Here's the foreach statement that is NOT randomized:
foreach (Transform child in transform)
{
Debug.Log(child);
}
Output:
child1
child2
child3
child4
What I want the output to be: (Randomized)
child4
child3
child1
child2
And yes. I know that even if it's randomized, the first output could still happen. But I don't want it to happen 100% of the time.
Is there a way I can do this? Thanks!
Answer by fafase · Dec 16, 2014 at 08:22 AM
You cannot randomize a foreach because it internally uses the order of the collection. But you can randomize your collection priorly:
Transform [] RandomChilds() // Here is a teaser for grammar nazis
{
List<Transform> list = new List<Transform> ();
foreach(Transform t in transform)
{
list.Add (t);
}
Transform [] array = list.ToArray ();
for (int i = 0; i < list.Count; i++)
{
int rand = Random.Range (0, list.Count);
Transform temp = array[rand];
array[rand] = array[i];
array[i] = temp;
}
return array;
}
void Awake ()
{
Transform [] t = RandomChilds ();
foreach (Transform tr in t) {
print (tr.name);
}
}
Yup, that's pretty much what I was getting at. Deleted $$anonymous$$e because this is actually implementable.
Also, this is fairly hardcoded and could benefit some more general use by removing Transform and using generic. But I felt it might get overwhel$$anonymous$$g not knowing the OP level.
Soooo ins$$anonymous$$d of a transform I changed it to a gameObject aaanndd there's this error:
error CS1579: foreach statement cannot operate on variables of type UnityEngine.GameObject' because it does not contain a definition for
GetEnumerator' or is inaccessible
this is the line of code:
foreach(GameObject g in gameObject)
This is because Transform implements IEnumerable interface to return children. GameObject does not as it would not have any suitable logic for implementation.
Answer by zharik86 · Dec 16, 2014 at 08:17 AM
For example, you try this:
void Start() {
//Create list for state of child into transform and initialization
List<int> tpChild = new List<int>; //maybe use bool list or array
for(int i = 0; i < this.transform.childCount; i++) {
tpChild.Add(i);
}
//Find random object
for(int i = 0; i < this.transform.childCount; i++) {
int num = Random.Range(0, tpChild.Count);
Debug.Log(this.transform.GetChild(tpChild[num]));
//Remove element from list
tpChild.RemoveAt(num);
}
}
I hope that it will help you.
Answer by andrew-lukasik · May 19, 2018 at 10:43 PM
List<Transform> transforms = new List<Transform>( gameObject.GetComponentsInChildren<Transform>() );
transforms.Sort( (a,b)=> Random.Range(-10,11) );
Your answer
Follow this Question
Related Questions
C# For loop in button to set gameObjects in array to active 1 Answer
Multiple Cars not working 1 Answer
foreach (or for-next loop) not updating local values 1 Answer
Converting foreach touch into for. 1 Answer
Distribute terrain in zones 3 Answers