So i have 10 game objects… (cubes)…and I want them to randomly reshuffle positions among themselves after an interval of every 120seconds (2 minutes)…what do I do please help.
Easily, Shuffle them that means swap source to destination, loop through the 10 game objects and generate a destination index and swap the destination game object with the current game object:
public static void Shuffle (GameObject[] gameObjects) {
for (int i = 0; i < gameObjects.Length; i++) {
// Find a random index
int destIndex = Random.Range (0, gameObjects.Length);
GameObject source = gameObjects[i];
GameObject dest = gameObjects[destIndex];
// If is not identical
if (source != dest) {
// Swap the position
source.transform.position = dest.transform.position;
// Swap the array item
gameObjects[i] = gameObjects[destIndex];
}
}
}
Thanks.
Ok since I am a programming noob a bit of explanation would be highly appreciated.
my cubes are named as cube1 cube2 etc
Create an Array of Cubes and use the function to shuffle them and their position.
Thanks.
How do i make the cubes reshuffle every 120 seconds…thanks
Use a Coroutine, or do it in a Update method, by Coroutine:
void Start () {
StartCoroutine ("WaitAndShuffle");
}
IEnumerator WaitAndShuffle () {
while (true) {
yield return new WaitForSeconds (120);
Shuffle (cubes);
}
}
Thanks a lot for your support …but please answer another question where should i apply this Coroutine script…thnks a ton
You need to apply the functions to where you have cubes and manage them, the MonoBehaviour script.
Thanks.
oK so i am using this script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class shuffle : MonoBehaviour {
public GameObject[ ] Cube;
public static void Shuffle (GameObject[ ] gameObjects) {
for (int i = 0; i < gameObjects.Length; i++) {
// Find a random index
int destIndex = Random.Range (0, gameObjects.Length);
GameObject source = gameObjects*;*
- GameObject dest = gameObjects[destIndex];*
- // If is not identical*
- if (source != dest) {*
- // Swap the position*
- source.transform.position = dest.transform.position;*
- // Swap the array item*
_ gameObjects = gameObjects[destIndex];_
* }*
* }*
* }*
but i am getting compiler errors please help
3338529–260557–shuffle.cs (643 Bytes)
Please read this page for how to post code on the forums: Using code tags properly
What compiler errors are you getting? When you post your code, please indicate which line it is, if it differences from the message you got in your own console…
okokok sorry i fixed the compiler errors
ok i fixed the compiler errors…but could not get the cubes shuffling randomly…whenever i hit the play button…the cubes do not reshuffle pls help
code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Randomshuffle : MonoBehaviour {
public GameObject[] cube;
public static void Shuffle (GameObject[] gameObjects) {
for (int i = 0; i < gameObjects.Length; i++) {
// Find a random index
int destIndex = Random.Range (0, gameObjects.Length);
GameObject source = gameObjects[i];
GameObject dest = gameObjects[destIndex];
// If is not identical
if (source != dest) {
// Swap the position
source.transform.position = dest.transform.position;
// Swap the array item
gameObjects[i] = gameObjects[destIndex];
}
}
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
Well, there are 2 issues I see here…
- is that the method is static but I imagine you want to access the cube array of game objects.
2… this is a big one. You haven’t actually called the method!
3rd issue, maybe: What happened to the timer you wanted before they shuffle? That is not here.
You want this to happen when you start? If you got rid of the timer idea, in Start() call the method with the cube array as a parameter.
Timer comes later bro first ill try to fix this
Okay, well try what I wrote and see if it works
Since i am a newbie…i did not understand what to do sorry for inconvenience.
Okay, I was suggesting that you remove the word ‘static’ from the method. And then in the Start() method, write "Shuffle(cube); "
Ok so I replaced line “8” with: “public static void Shuffle (GameObject[ ] gameObjects) {”
And in line “29” i wrote:
“Shuffle(cube);”
But i get a behaviour which i dont want
In my scene i have 3 cubes (its just a test scene for my game)
Starting from left to right
The first cube is red second is green and the last one is blue…after applying the script whenever i hit “play” everytime a random number of cubes appear (but less than 3) sometimes 2 sometimes only one appears with any of the three colours…
What i want is as soon as i hit “play” the number of cubes should not change but the three cubes should just interchanve their positions at random…thats it…need help please.
*interchange