Random game object reshuffling.

Not sure about your response, as you said you replaced the line with the exact same line…

Anyways…let’s see what we can do about the method.

public 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];
  /** This could be modified to keep checking to make sure you always swap something, but leaving it for now **/
            // If is not identical
            if (source != dest) {

                // Swap the positions
                Vector3 tmp = source.transform.position;
                source.transform.position = dest.transform.position;
                dest.transform.position = tmp;
                 
             /** I do not know what this was doing, try with it commented out! **/
                // Swap the array item
              //  gameObjects[i] = gameObjects[destIndex];
            }
        }
    }
void Start() {
   Shuffle(cube);
  }
void Update() {
  if(Input.GetKeyDown(KeyCode.L)) {
     Shuffle(cube);
    }
 }

Okay, try to copy & paste that and see what happens. if I made a typo, try to fix it.
I added some code so you can type the ‘L’ key to run it, again. It will also run once on start.
Let me know how that goes.

I am so sorry i did not write static

But i wrote a code like yours but it did not work

No problem, I figured you didn’t, because otherwise it would not have run :slight_smile:

Just try to paste the code I wrote and see what happens…

It’s working for me. Sometimes they just end up in the same spot, or only 1 moves… that’s random chance.
:slight_smile:

I just tested it in a project**

Ok ill test it tomorrow. …dont leave connection with this thread i have more problems…thanks for ur support…

nods, no problem. Try the bit of code with the option to run it , again (the GetKeyDown in Update), while you test.
Maybe then you can better see for yourself how sometimes ‘x’ number of them move (or not) :slight_smile: Much easier, for testing, than to stop/start… stop/start… over n over, again. :slight_smile:

Since i was too lazy to type…lol…pls see this i want something like this…

oh my goodness, that’s some severe laziness and it’s sideways lol

All I can say , again , is try the test code and also try the Update KeyDown to test it several times during play to see the results. :slight_smile: lol

If , after that, it’s still not making sense, you will have to make an effort to describe your goal a little more, because i am not seeing it very well at the moment, or when I tilt my head to read that note pad hahaha. Sorry :slight_smile:

ok so i pasted ur script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Randomshuffle : MonoBehaviour {
    public GameObject[] Cube;

    public 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];
            /** This could be modified to keep checking to make sure you always swap something, but leaving it for now **/
            // If is not identical
            if (source != dest) {

                // Swap the positions
                Vector3 tmp = source.transform.position;
                source.transform.position = dest.transform.position;
                dest.transform.position = tmp;

                /** I do not know what this was doing, try with it commented out! **/
                // Swap the array item
                gameObjects[i] = gameObjects[destIndex];
            }
        }
    }
    void Start() {
        Shuffle(cube);
    }
    void Update() {
        if(Input.GetKeyDown(KeyCode.L)) {
            Shuffle(cube);
        }
    }

it shows "errorCS1525 ; unexpected symbol ‘end of file’

You’re missing a closing curly bracket at the end…

Also this:

/** I do not know what this was doing, try with it commented out! **/
// Swap the array item
gameObjects[i] = gameObjects[destIndex];

You’re supposed to be swapping the objects… but you don’t have every part to it. This single line of code is just going to remove one of the GameObjects, and have the other in 2 spots.

Do this:

var placeHolder = gameObjects[destIndex];
gameObjects[destIndex] = gameObjects[i];
gameObjects[i] = placeHolder;

This actually swaps the 2 around so that the object at destIndex is now at i, and the one at i is not at destIndex.

Is that swap really necessary? I had commented the first part out, because I had added a ‘swap’ that wasn’t in the earlier code posted by someone else. Half just wondering out loud, half asking :slight_smile:

umm swap is needed

I’m sorry that message wasn’t for you, but for @lordofduct . It wasn’t about needing swap in general, but that particular piece of code in his post* :slight_smile:

In my code I commented out one line that in your post wasn’t commented out. So you can try that, after you add the curly brace that was giving you the error. Then, if you want, try the version that was written with the “other swap” added… :slight_smile:

I have always run under the concept that if you want something shuffled, treat it like a deck of cards… You basically have 2 stacks… 1 is the source, as you take a card randomly from stack one and add it to stack 2, remove it from stack 1.

The best illustration is to simply create a extension class that does the work for you…

using System.Collections.Generic;
using System.Linq;

public static class Extensions{
	public static List<T> Shuffle<T>(this List<T> source) {
		var rtn = new List<T>();
		var copy = source.ToList();
		var rnd = new Random();
		
		while (copy.Any()) {
			var index = rnd.Next(copy.Count);
			rtn.Add(copy[index]);
			copy.RemoveAt(index);
		}

		return rtn;
	}
	
	public static T[] Shuffle<T>(this T[] source)
	{
		return source.ToList().Shuffle().ToArray();
	}
}

//Usage:

var children = new List<Transform>();
for(var i=0; i<transform.childCount; i++){
	children.Add(transform.GetChild(i));
}

var random = children.Shuffle();

I’m glad you responded & posted that. That is something I had thought of and/or read about before, and really liked, but had slipped my mind, as I was using an earlier shuffle I had grown used to. :slight_smile: Cool.

(not the exact code, but the idea of it**)

Ok…so do i need to creat any array for this (i am getting a bit confused)

Lol…confused person here

Well, yes, you must create an array to start. Using just about any one of these methods, you need an initial array of objects in order to shuffle them.

In my example, I simply pulled the array of objects from the children of the object that script would be on. So, yes, you need an array.

And what is that “T”

…like we have here