- Home /
Swap array - list transform.positions
Thank You in advance! I have a grid of instantiated gameobject's and have tagged them as master, I created an array from this 7x7 grid and created a list from those. I am attempting to get the transform.positions from this list and shuffle the list and then replace the grid with the shuffled transform.positions.
IEnumerator shuffleGrid(){
GameObject[] gms = GameObject.FindGameObjectsWithTag ("Master");
List<GameObject> transformsLoc = new List<GameObject>();
transformsLoc.AddRange(gms);
foreach (GameObject gm in transformsLoc)
{
Vector3 temp = gm.transform.localPosition; //this gets the list of all transform.positions - just no idea how to randomize the list of these
print(temp);
}
}
yield return null;
}
I have tried many diff things from an extensive google and unity3d search but cant seem to find any good ideas. Thank You again for any assistance AS
This does not belong in the $$anonymous$$ETA area. $$anonymous$$oving it to Default for you...
Answer by jgodfrey · Jun 13, 2016 at 11:55 PM
Why not simply shuffle the array itself and skip the list generation? There are several standard algorithms for that and lots of C# code on the net. For instance, there's some options here:
http://stackoverflow.com/questions/108819/best-way-to-randomize-an-array-with-net
... of special interest might be the answer from Matt Howells
or here...
http://csharphelper.com/blog/2014/07/randomize-arrays-in-c/
or, here's one that shuffles a list...
http://www.vcskicks.com/randomize_array.php
It shouldn't be too difficult to find something that suits your needs...
Your answer
![](https://koobas.hobune.stream/wayback/20220612082900im_/https://answers.unity.com/themes/thub/images/avi.jpg)