How I randomise a list of signals in Unity and remove it from the list once the clip is finished?
I have 4 audio clips. When I play a press a certain button in Unity, the data is sent to MAX/MSP and the audio is played. I created a list a signals (As shown below in the code). What I want is that, the signal list is randomised. The user presses a button to listen to the audio (which works fine). Once the clip is finished the user presses Next Trial button. Once the Next Trial is pressed. The previous clip is removed from the list. Once all the clips are finished. A message is displayed, in the Debug.Log("All trials finished"). I have the following problems:
1) One of the audio clips is repeated again. It appears that the audio clips are not removed from the list.
2) How do I write the audioclip index to a csv file?
My code is as follows:
public class SignalToMax : MonoBehaviour
{
private static int localPort;
private string IP; // define in init
public int port = 8050; // define in init
// "connection" things
IPEndPoint remoteEndPoint;
UdpClient client;
// experiment case
public string experimentCase = "A";
string strMessage = "";
public List<int> signals;
public static int stimIndex;
}
void Start()
{
NextTrial = GameObject.FindGameObjectWithTag("NextTrial").GetComponent<Button>();
signals = new List<int> { 1, 2, 3, 4 };
// define
IP = "127.0.0.1";
port = 8050;
// ----------------------------
// Senden
// ----------------------------
remoteEndPoint = new IPEndPoint(IPAddress.Parse(IP), port);
client = new UdpClient();
NextTrial.onClick.AddListener(Advance_Trial);
}
void Advance_Trial()
{
int signalIndex = Random.Range(0, signals.Count);
int currentSignal = signals[signalIndex];
stimIndex = currentSignal;
signals.RemoveAt(signalIndex);
string text = signalIndex.ToString();
byte[] data5 = Encoding.ASCII.GetBytes(text);
client.Send(data5, data5.Length, remoteEndPoint);
Debug.Log("<color=magenta>Current source signal is:</color>" + currentSignal);
if (signals.Count > 0)
{
Debug.Log("<color=blue>Signal count before removal was:</color>" + signals.Count);
Debug.Log("<color=yellow>Source signal removed</color>");
}
else
{
Debug.Log("<color=green>All Trials finished</color>");
}
}
Your answer
Follow this Question
Related Questions
C# Having troubles with creating/referencing a random list. 1 Answer
Random.Range returns either 1 or 2 2 Answers
Sort a List with a public class 0 Answers
How do I get random answers? 2 Answers