stack overflow exception error with Random.Range

Hi.

I have 9 characters in a row.

I want to re-fill the row with characters(spawn) when existing characters move away.

Here’s the script to find out which spot is empty.

it works but sometimes it causes “stack overflow exception” error.(Line 19)

I googled it and found it’s because this script uses a recursive method(?)

can anyone tell me how to fix this?

 public class LumiInfo
     {
         public GameObject lumies;
     }
    
 public LumiInfo[] lumiInfo;
 public GameObject[] lumiPrefab;
 public GameObject[] readyLumies;
 private Vector3[] tr;
 public List<int> ttt = new List<int>();

 void Start()
 {
        tr[0] = lumiInfo[0].lumies.transform.position;
		tr[1] = lumiInfo[1].lumies.transform.position;
		tr[2] = lumiInfo[2].lumies.transform.position;
		tr[3] = lumiInfo[3].lumies.transform.position;
		tr[4] = lumiInfo[4].lumies.transform.position;
		tr[5] = lumiInfo[5].lumies.transform.position;
		tr[6] = lumiInfo[6].lumies.transform.position;
		tr[7] = lumiInfo[7].lumies.transform.position;
		tr[8] = lumiInfo[8].lumies.transform.position;
}
 
 void Update()
 {
    readyLumies = GameObject.FindGameObjectsWithTag("READY"); // to find characters that didn't make move yet.
 }
 
 public void SpawnLumi()
     {
         int t = Random.Range(0, 8); // <-- stack overflow exception error!!!
         if(checkIfPosEmpty(tr[t]) && t != ttt.Find(i=> i>-1) && t != ttt.FindLast(i=> i>-1))
             {
                 lumiInfo[t] = new LumiInfo();
                 int randomCount = Random.Range(0,3);
                 lumiInfo[t].lumies = (GameObject) Instantiate(lumiPrefab[randomCount],tr[t], Quaternion.Euler (0,0,0));
              lumiInfo[t].lumies.gameObject.tag = "READY";
                 ttt.Add(t); //a list to make sure t don't have same number on next run.
                      
                 StartCoroutine(ClearList());
                 
             return;
 
             }else{
                     SpawnLumi();                
             }
     
     }
 
     public bool checkIfPosEmpty(Vector3 targetPos)
         { 
               foreach(GameObject current in readyLumies)
                 {
                     if(current.transform.position == targetPos)
                     return false;
                 } 
 
                     return true;
 
         } 
 

IEnumerator ClearList()
{
   yield return new WaitForSeconds(0.5f);
   ttt.Clear();
}

Just create a loop insteed of a recursive function.

      public void SpawnLumi()
      {
          int t = 0;
          while (t < 9) {
              if(checkIfPosEmpty(tr[t]) && t != ttt.Find(i=> i>-1) && t != ttt.FindLast(i=> i>-1))
              {
                  lumiInfo[t] = new LumiInfo();
                  int randomCount = Random.Range(0,3);
                  lumiInfo[t].lumies = (GameObject) Instantiate(lumiPrefab[randomCount],tr[t], Quaternion.Euler (0,0,0));
                  lumiInfo[t].lumies.gameObject.tag = "READY";
                  ttt.Add(t); //a list to make sure t don't have same number on next run.
                       
                  StartCoroutine(ClearList());
                  
              return;
  
              }
          t++;
          }
      
      }

I have just remove the recursive from your function. You should remove also your verification of “t” (verification and list…) because in the loop it will be never the same…