- Home /
Connect object's position to shuffled array
I would like to randomly assign a set of positions to some GameObjects. Example: I have 4 different kinds of Cubes, when the game starts,each one should go to one of 4 positions on a table (like a grid), and next time the game starts the cubes changes positions. (The Positions are the same, the objects just change places with eachother.) I have managed to create an Array of int numbers and shuffle that, I can see the orders of the numbers change in the inspector. (It was from the Unity manual example and a tutorial on Fisher Yates shuffle. What I would like to know, is it possible to connect the numbers from that Array to an Array of GameObjects transform postions? Like Array element [0] = GO [0] transform.position x,y,z... Or is it possible to make the Fisher Yates shuffle with an Array of Gameobjects or Transform or Vector3 Array? I Think I've searched around for Three Days on this so I would be deeply grateful for just some help... Here's what I have so far;
public int[] Numbers = new int [4];
void Start ()
{
Shuffle (Numbers);
}
void Shuffle(int[] a)
{
for (int i = a.Length-1; i > 0; i--)
{
int rnd = Random.Range (0,i);
int temp = a[i];
a[i] =a[rnd];
a[rnd] = temp;
}
for (int i = 0; i < a.Length; i++)
{
Debug.Log (a[i]);
Answer by robertbu · Feb 20, 2014 at 02:11 AM
Any of what you suggest is possible. I'd shuffle the array of game objects, or the array of positions. The only change you would have to make is the type:
void Shuffle(GameObject[] a)
{
for (int i = a.Length-1; i > 0; i--)
{
int rnd = Random.Range (0,i);
GameObject temp = a[i];
a[i] =a[rnd];
a[rnd] = temp;
}
}
Then you would just assign the positions in order:
for (i = 0; i < goArray.Length; i++) {
goArray[i].transform.position = positions[i];
}
But you could do it with shuffling an array of numbers. Assume you had shuffled the array called num. You could do:
for (i = 0; i < goArray.Length; i++) {
goArray[num[i]].transform.position = positions[i];
}
Great! Thank you so much! I would have Upvoted the answer if I could. The first part I got all right at once, after some sleep I also managed to understand how to get the transform positions right too. I made a public Vector 3 Array variable named positions:
public Vector3 [] positions = new Vector3 [4]; and then it was easy to assign the cubes to those positions elements, and I adjusted the last bit of code (with "int" before i) and it worked perfect! (I added a debug where you can follow how they change, it is also visible in the inspector.)
for (int i = 0; i < Cubes.Length; i++) { Debug.Log("Cube number "+i+" is named "+Cubes[i].name); Cubes[i].transform.position = positions[i]; }
This is the most underrated answer I've ever seen! This helped me perfectly! Thank you so much!
Your answer
Follow this Question
Related Questions
How do I randomize the order of spawn points without repeats? 1 Answer
Setting GUI textures 1 Answer
Find Active gameobject with tag and store in a List/Array as a Transform? 3 Answers
Iterating through shuffled/randomized array issue 0 Answers
Spawning - How to only have one object at a spawn at a time? 0 Answers