- Home /
I want to swap randomly 5 blocks (block has 5 diffrent color) and spawn in vertical line
i have 5 blocks(5 diffrent color) and i want to swap randomly it before spawning it how i can do that @dev-waqas @Tespy @pako @Yo318
Answer by OneCept-Games · Jan 02, 2018 at 02:14 PM
In Pseudo code: var MyArray = new Array(1, 2, 3, 4, 5); var MyIndex = Random.Range(0,(MyArray.length - 1)); Debug.Log(MyArray[MyIndex]);
For every loop, remove the MyArray[MyIndex] from the array, and you will never reuse the already used position. You migt consider using a List instead, since you have Add, Remove methods, where Arrays require some more housekeeping.
can u share a page of code (c #) still in confusion(spawning in vertical line is done in swaping i dont know what to do) @oneCept-Games thnx for anwser
I am not at a Unity Seat right now, but something like (in C#):
void SpawnVertical(int numberOfObjects) {
List objectsToSpawn<int> = new List<int>(numberOfObjects);
// Prepare the List
for (int i=0; i<numberOfObjects; i++) {
objectsToSpawn.Add(i);
}
// Spwan the objects vertically
int cnt=numberOfObjects;
for (int i=0; i<cnt; i++) {
int idx = Random.Range(0, cnt-1);
int x = objectsToSpawn.Item(idx);
objectsToSpawn.RemoveAt(idx);
Debug.Log(x);
cnt--;
}
}
Your answer
Follow this Question
Related Questions
i want to spawn randomly 1 Answer
Problems with array "shuffle bag" 1 Answer
Spawning random questions at a time 1 Answer
Simple 2D Enemy AI 3 Answers
Transform.position without Vector3? 1 Answer