What I think you are asking for is “How to get a random index from a set of indexes without repeated indexes?”
I am going to assume you can refer to your scenes with indexes, so this is what you want to do.
- Make a array of bools(array size 8 for old)
- Get a random index from within that array(int index = Random.Range(0, array.length))
- Check is array[index] == false
- If true: set array[index] = true and return your index.
- If false: repeat from 2.
I wrote a script that might help you. This is not tested but you get the overall idea. I have also included a way to save these arrays so that you can save progress between experiments.
using UnityEngine;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class RandomIndexSelector : MonoBehaviour
{
public bool[] usedIndexPool;
/// <summary>
/// The name of the file. Leave Empty if you do not want to save
/// </summary>
public string fileName;
/// <summary>
/// Awake this instance.
/// </summary>
private void Awake()
{
if(fileName != "")
{
Load();
}
else
{
Reset();
}
}
#region Operations
/// <summary>
/// Reset usedIndexPool.
/// </summary>
public void Reset ()
{
for (int i = 0; i < usedIndexPool.Length; i++) {
usedIndexPool *= false;*
-
}*
-
}*
-
/// <summary>*
-
/// Gets a new index from the index pool.*
-
/// </summary>*
-
/// <returns>The index or -1 if index has not found</returns>*
-
public int GetIndex ()*
-
{ *
-
bool hasActiveIndex = false;*
-
for (int i = 0; i < usedIndexPool.Length; i++) {*
_ if (usedIndexPool == false) {_
* hasActiveIndex = true;*
* break;*
* }*
* }*
* if (hasActiveIndex == false) {*
* return -1;*
* }*
* while (true) {*
* int index = Random.Range (0, usedIndexPool.Length);*
* if (usedIndexPool [index] == false) {*
* usedIndexPool [index] = true;*
* //if you want to save every time a new index is found*
_ /_
_ if(fileName != “”)_
_ {_
_ Save();_
_ }*_
_ */_
* return index;*
* }*
* }*
* }*
#endregion
#region Save and Load data
* //saves data to disk*
* public void Save ()*
* {*
* BinaryFormatter bf = new BinaryFormatter ();*
* FileStream file = File.Create (Application.persistentDataPath + “/” + fileName + “.dat”);*
* DataContainer dc = new DataContainer(usedIndexPool);*
* bf.Serialize (file, dc);*
* file.Close ();*
* }*
* //loads data from disk*
* public void Load ()*
* {*
* if (File.Exists (Application.persistentDataPath + “/” + fileName + “.dat”)) {*
* BinaryFormatter bf = new BinaryFormatter ();*
* FileStream file = File.Open (Application.persistentDataPath + “/” + fileName + “.dat”, FileMode.Open);*
* DataContainer dc = (DataContainer)bf.Deserialize (file);*
* file.Close ();*
* //Loads the saved data to the active instance*
* usedIndexPool = new bool[dc.usedIndexPool.Length];*
* for (int i = 0; i < usedIndexPool.Length; i++) {*
usedIndexPool = dc.usedIndexPool ;
* }*
* } else {*
* Debug.Log (“No save data found”);*
* Reset ();*
* }*
* }*
* public class DataContainer*
* {*
* public bool[] usedIndexPool;*
* public DataContainer(bool[] uip)*
* {*
* usedIndexPool = new bool[uip.Length];*
* for(int i = 0; i < uip.Length)*
* {*
usedIndexPool = uip*;*
* }*
* }*
* }*
#endregion
}
And this is how you use it.
using UnityEngine;
using System.Collections;
public class UseCase : MonoBehaviour
{
* //set newPool and oldPool from the inspector*
* //newPool size = 168, newPool filename and oldPool size = 8*
* public RandomIndexSelector newPool;*
* public RandomIndexSelector oldPool;*
* // Update is called once per frame*
* void Update ()*
* {*
* if(Input.GetKeyDown(KeyCode.N))*
* {*
* int newIndex = newPool.GetIndex();*
* Debug.Log("NewIndex: " + newIndex);*
* if(newIndex == -1)*
* {*
* Debug.Log(“No more active indexes in new pool”);*
* }*
* }*
* if(Input.GetKeyDown(KeyCode.O))*
* {*
* int oldIndex = oldPool.GetIndex();*
* Debug.Log("OldIndex: " + oldIndex);*
* //check is oldIndex is valid*
* if(oldIndex == -1)*
* {*
* Debug.Log(“No more active indexes in old pool”);*
* //oldPool.Reset*
* //or*
* //end of test*
* }*
* }*
* }*
}