Display objects in a random sequence ?

I`m trying to display objects in a random sequence. When one object appears and if you trigger it (the objects which I randomize have tirggers on them) with another object, that current active object will become inActive and the next object will appear… then the same for all 4 objects . All 4 objects have fixed coordinates.

My 1.potential solution is :
making a randomized boolean array , like bool_array[true, false, false, false] and by creating the boolean array there will have to be only one true statement , If that is even possible? The problem is that new version of the bool_array would have to be instantiated every time to display different object, but dont know where in the code i would do that ?

My 2. potential soluition:
After creating an array which generates random numbers (1-4) with no double numbers, is to somehow concatenate the randomArray[0] in the name of the objectToActivate+randomArray[0] then accordingly turn the object on/off ?

I may be going about this in a completly wrong way, my mind is doing backflips btw …probably there is and must be a WAY more sensible or logical solution for this. Sorry for being a noob :frowning:

Any help would really be appreciated ! :smiley:

    public GameObject objectToActivate1;
    public GameObject objectToActivate2;
    public GameObject objectToActivate3;
    public GameObject objectToActivate4;



 void start(){

//myrandom array
randomArray = new List<int>(new int[Lenght]);

    for (int j = 1; j < Lenght; j++)
    {
        Rand = Random.Range(1, 5);

        while (randomArray.Contains(Rand))
        {
            Rand = Random.Range(1, 5);
        }

        randomArray[j] = Rand;
       
    }
    //here is where i would create the boole array and activate the one object...
    }
 void Update()
    {
        
        if (visible == false)
        {
        objectToActivate1.SetActive(bool_array[0]);
        objectToActivate2.SetActive(bool_array[1]);
        objectToActivate3.SetActive(bool_array[2]);
        objectToActivate4.SetActive(bool_array[3]);

              
        }
       
    }

//detects collisions on all objects
    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Gloves") {
            
            SoundSource.Play();
            visible = false;
             
        }
    }

You overcomplicate the issue in several ways. First of all as soon as you have more than 2 similar “things” of anything you should use an array / List and not individual variables. Second the way you fill your array with random numbers has a horrible runtime performance and if a limit is choosen wrongly will result in an infinite loop. Additionally the code / pseudo code you posted won’t compile and missing a lot of details. In most programming languages we start counting at 0. Since you start your loop at 1 you will be one element short.

I don’t see anything in your code that resembles an actual sequence. However the easiest solution is to create a copy of the array / list of objects and just shuffle it. If the order of the objects in the original array do not matter to you at all you can just shuffle the original array. The most easiest way to shuffle an array is the [Fisher-Yates shuffle][1]. [See an implementation here][2]

So just do:

public GameObject[] objectsToActivate;

void Start()
{
    Shuffle();
}
void Shuffle()
{
    for (int i = 0; i < objectsToActivate.Length; i++)
    {
        GameObject temp = objectsToActivate*;*

int randomIndex = Random.Range(i, objectsToActivate.Length);
objectsToActivate = objectsToActivate[randomIndex];
objectsToActivate[randomIndex] = temp;
}
}
Now everytime you call Shuffle (which is called once automatically at start) the whole array is shuffled. So you can simply walk through your array linearly. Since we don’t know anything about how you want to activate your sequence, where this script is attached to, etc… we can’t really get more into details.
[1]: Fisher–Yates shuffle - Wikipedia
_*[2]: http://answers.unity.com/answers/487245/view.html*_

// Top of the file
using System.Collections.Generic;
using UnityEngine;
using System.Linq;

    // ....

    public GameObject[] objectsToActivate;
    private Stack<int> objectsIndices;

    //detects collisions on all objects
    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Gloves")
        {
            SoundSource.Play();
            ShowRandomObject();
        }
    }

    private void ShowRandomObject()
    {
        if (objectsIndices == null || objectsIndices.Count == 0)
            FillObjectsIndices();
        int index = objectsIndices.Pop();
        for (int i = 0; i < objectsToActivate.Length; i++)
        {
            objectsToActivate*.SetActive(i == index);*

}
}

private void FillObjectsIndices()
{
System.Random random = new System.Random();
objectsIndices = new Stack();
for (int i = 0; i < objectsToActivate.Length; ++i)
objectsIndices.Push(i);
objectsIndices = new Stack( objectsIndices.OrderBy(x => random.Next()) );
}