In my Unity Project, I have list of 4 signals. In the code, when CheckPress() is called, a random index, what I call the firststimIndex is selected from a list of signals and that index is sent to MAX/MSP via UDP to play some audio. When PlaySignal() is called, the audio associated with this index is played. This works fine. Then, when LoadNextSignal() is called, this previous index is removed and a new random index is loaded and played and is removed. LoadNextSignal() needs to be called 3 times as the remaining number of signals are three. This process will continue until all signals from the list are removed.

My problem lies when LoadNextSignal()is executed when called second time. How do I fix this part of my code? Also, The variable stimIndex is required as in my project when I write the data, it is written as signals[stimIndex].

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Text;
using System.Net;
using System.Net.Sockets;

public class CheckButtonBehaviour : MonoBehaviour
public Button Check;
public Button Play;
public Button NextSignal;

public List<string> signals = new List<string> {"Speech1", "Speech2", "Speech3", "Speech4"};

public int stimIndex;
public int index = 1;
public int counter = 3;
public int remainingSignals;

private static int localPort;
private string IP;  
public int port = 8050;
IPEndPoint remoteEndPoint;
UdpClient client;

void Start()
    IP = "";
    port = 8050;
    remoteEndPoint = new IPEndPoint(IPAddress.Parse(IP), port);
    client = new UdpClient();

public void CheckPress()    
    var random = new System.Random();
    int firststimIndex = random.Next(signals.Count);

    ///Sends to MAX MSP 
    string text = firststimIndex.ToString();
    byte[] data5 = Encoding.ASCII.GetBytes(text);
    client.Send(data5, data5.Length, remoteEndPoint);
    Debug.Log("<color=red>First signal is : </color>"+ signals[firststimIndex]);
    stimIndex = firststimIndex;
    Check.interactable = false;
    Check.enabled = false;


public void LoadNextSignal()
   signals.RemoveAt(stimIndex); //Removes the previous signal
   remainingSignals = signals.Count;

   Debug.Log("<color=red>Remaining Signals are : </color>" + remainingSignals);
   int randomIndex = UnityEngine.Random.Range(0, remainingSignals);
   string text1 = randomIndex.ToString(); 
   byte[] data6 = Encoding.ASCII.GetBytes(text1);
   stimIndex = randomIndex;
   client.Send(data6, data6.Length, remoteEndPoint);
   Debug.Log("Loaded Signal" + stimIndex + "; remaining signals before removal equals" + remainingSignals);

    if (counter >= index)
        Debug.Log("<color=orange>Yay! Keep Listening!</color>");
        Debug.Log("<color=green>All Trials finished</color>");

void PlaySignal()
    byte[] data2 = Encoding.ASCII.GetBytes("A"); //B
    client.Send(data2, data2.Length, remoteEndPoint);
    Debug.Log("<color=red>Signal is Played</color>");
  • 1
    \$\begingroup\$ Did you mean to remove the item twice? Once right after selecting it (signals.RemoveAt(randomIndex);), and again when you're about to select the next item (signals.RemoveAt(stimIndex); //Removes the previous signal)? \$\endgroup\$ – DMGregory Nov 19 at 14:47
  • \$\begingroup\$ No, I do not mean to remove them twice. The first signal index is removed once and then the same happens for the remaining signals. \$\endgroup\$ – Tanmayee Pathre Nov 19 at 14:52
  • \$\begingroup\$ Sounds like that's your bug then. \$\endgroup\$ – DMGregory Nov 19 at 15:01
  • \$\begingroup\$ @DMGregory, is it possible that I can call signals.RemoveAt(stimIndex), outside the LoadNextSignal() function. I cannot call this function inside CheckPress(). \$\endgroup\$ – Tanmayee Pathre Nov 19 at 15:10
  • 2
    \$\begingroup\$ gamedev.stackexchange.com/questions/117103/… check the draw function, unique draw without repeat but without the delete of items \$\endgroup\$ – Zibelas Nov 19 at 15:21

Browse other questions tagged or ask your own question.