- Home /
i want to swap my blocks
A bit more information would be welcome. Which blocks do you want to "swap"? And what do you mean by "swap"?
i want to shuffle randomly 5 blocks as shown in image @Harinezumi
Answer by Harinezumi · Jan 25, 2018 at 02:58 PM
Assuming that what you mean is to shuffle the game objects as with a deck of cards, you need a shuffle algorithm, and assigning the positions:
public void ShuffleObjects(GameObjects[] toShuffle) {
// store the original position of each element
Vector3[] positions = new Vector3[toShuffle.Length];
for (int i = 0; i < toShuffle.Length; ++i) { positions[i] = toShuffle[i].transform.position; }
Shuffle(toShuffle);
// assign the original positions to the shuffled array
for (int i = 0; i < toShuffle.Length; ++i) { toShuffle[i].transform.position = positions[i]; }
}
public static void Shuffle(GameObject[] array) {
int count = array.Length;
int last = count - 1;
for (int i = 0; i < last; ++i) {
// select a random index to move to
int randomIndex = UnityEngine.Random.Range(i, count);
// swap the elements
GameObject temp = array[i];
array[i] = array[randomIndex];
array[randomIndex] = temp;
}
}
See this post for a more generic shuffling solution.
I A$$anonymous$$ NEW I DONT $$anonymous$$NOW WHERE TO PUT THIS CODE to blocks?? @Harinezumi
Create a script and add the above code to it. Then when you want to shuffle your objects, call ShuffleObjects(yourArrayOfObjects);
on this script. You need to create yourArrayOfObjects
, the easiest way being declaring a public GameObject[] yourArrayOfObjects;
and in the inspector assigning your objects to it.
This should get you going. I strongly recommend that you look up some basic tutorials for Unity to make it easier for you to understand the answers.
Good luck!
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class rop : $$anonymous$$onoBehaviour {
public GameObject[] yourArrayOfObjects;
public void ShuffleObjects(GameObjects[] toShuffle) {
// store the original position of each element
Vector3[] positions = new Vector3[toShuffle.Length];
for (int i = 0; i < toShuffle.Length; ++i) { positions[i] = toShuffle[i].transform.position; }
Shuffle(toShuffle);
// assign the original positions to the shuffled array
for (int i = 0; i < toShuffle.Length; ++i) { toShuffle[i].transform.position = positions[i]; }
}
public static void Shuffle(GameObject[] array) {
int count = array.Length;
int last = count - 1;
for (int i = 0; i < last; ++i) {
// select a random index to move to
int randomIndex = UnityEngine.Random.Range(i, count);
// swap the elements
GameObject temp = array[i];
array[i] = array[randomIndex];
array[randomIndex] = temp;
}
}
} any problem in this code
Your answer
Follow this Question
Related Questions
I want to swap 5 blocks randomly on same position. 2 Answers
Instantiate a button at mouse click position, 0 Answers
Instantiated Object Wont save a reference to the prefab, instead sets to itself 1 Answer
How to Spawn after checking if the clones are destroyed. 1 Answer
[Solved] How to instantiate GameObject with different rigidbody parameters 1 Answer