Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Coroutine + List not working

Discussion in 'Getting Started' started by perritoabrazable123, Dec 7, 2021.

  1. perritoabrazable123

    perritoabrazable123

    Joined:
    Oct 27, 2020
    Posts:
    2
    I am new to Unity and coding in general, I made this script for a project of mine, what it is supposed to do
    is make a list of 6 different randomly chosen numbers, it seems to be working well except for two things, I get an infinite loop and I think that the coroutine that I marked as failedRoutine is not stopping, causing a massive list creation.

    Somebody can tell me what I am doing wrong?


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TESTING : MonoBehaviour
    6. {
    7.  
    8.     private void Start()
    9.     {
    10.         currentRoutine = StartCoroutine(CreateShop(0));
    11.     }
    12.     Coroutine currentRoutine;
    13.  
    14.     Coroutine failedRoutine;
    15.  
    16.     List<float> itemsAlreadyInShop = new List<float>();
    17.     IEnumerator CreateShop(float times)
    18.     {
    19.  
    20.  
    21.         float number;
    22.  
    23.  
    24.         yield return new WaitForSeconds(0.25f);
    25.  
    26.         if (times > 5)
    27.         {
    28.  
    29.         }
    30.         if (times < 6)
    31.         {
    32.  
    33.  
    34.             number = Random.Range(1, 12);
    35.  
    36.            
    37.             itemsAlreadyInShop.Sort();
    38.             foreach (float item in itemsAlreadyInShop)
    39.             {
    40.                 if (item == number)
    41.                 {
    42.                     float stringTimes;
    43.                     stringTimes = times + 1;
    44.                     print("Routine " + 1 + stringTimes.ToString() + " failed because the randomized number " + number.ToString() + " equals our list item " + item.ToString());
    45.                     failedRoutine = currentRoutine;
    46.                     currentRoutine = StartCoroutine(CreateShop(times));
    47.                     StopCoroutine(failedRoutine);
    48.                 }
    49.             }
    50.  
    51.  
    52.             itemsAlreadyInShop.Add(number);
    53.             print("new item in list! number: " + number.ToString());
    54.  
    55.            
    56.  
    57.             times++;                          
    58.             print(times + " rutine is done");
    59.             print("item is " + number);
    60.             print("current list is:");
    61.             foreach (float item in itemsAlreadyInShop)
    62.             {
    63.                 print(item.ToString());
    64.             }
    65.             currentRoutine = StartCoroutine(CreateShop(times));
    66.         }
    67.     }
    68.  
    69. }
     
  2. perritoabrazable123

    perritoabrazable123

    Joined:
    Oct 27, 2020
    Posts:
    2
    I already solved the problem using a different method