Search Unity

how to insert coroutine along with case

Discussion in 'Scripting' started by carnag45, Jan 16, 2020.

  1. carnag45

    carnag45

    Joined:
    May 22, 2019
    Posts:
    25
    I have a check between 0 and 3, which are the 3 cases, I am trying to insert a routine, but in doing so I can no longer access my cases.


    Code (CSharp):
    1.     public int index;
    2.     private void Start()
    3.     {
    4.         StartCoroutine(indice());
    5.     }
    6.  
    7.     public void previous()
    8.     {
    9.         index--;
    10.         voidindex();
    11.     }
    12.  
    13.     public void next()
    14.     {
    15.         index++;
    16.         voidindex();
    17.     }
    18.  
    19.     void voidindex()
    20.     {
    21.         if (index <= 0)
    22.         {
    23.             index = 0;
    24.         }
    25.  
    26.         if (index >= 3)
    27.         {
    28.             index = 3;
    29.         }
    30.     }
    31.  
    32.     IEnumerator indice()
    33.     {
    34.         switch (index)
    35.         {
    36.             case 0:
    37.             {
    38.                 yield return new WaitForSeconds(0.1f);
    39.                 print("0");
    40.             }
    41.  
    42.             break;
    43.             case 1:
    44.             {
    45.                 yield return new WaitForSeconds(0.1f);
    46.                 print("1");
    47.             }
    48.  
    49.             break;
    50.  
    51.             case 2:
    52.             {
    53.                 yield return new WaitForSeconds(0.1f);
    54.                 print("2");
    55.             }
    56.  
    57.             break;
    58.  
    59.             case 3:
    60.             {
    61.                 yield return new WaitForSeconds(0.1f);
    62.                 print("3");
    63.             }
    64.  
    65.             break;
    66.         }
    67.     }
    68. }
     
    Last edited: Jan 16, 2020
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    What do you mean you can't access your cases?
    You're missing two closing brackets, but I'm guessing that's copy/paste error.

    As far as what I'm seeing, you are trying to pass an int to your coroutine, but you also appear to have a field(class variable) named index, so there isn't a reason to pass it in. Your coroutine is trying to use the parameter. Just because they are named the same doesn't mean they are the same. Remove the int index from the coroutine (unless you actually mean to pass it in, in which case, show the rest of your script)
     
  3. carnag45

    carnag45

    Joined:
    May 22, 2019
    Posts:
    25
    not much to complement, the only thing missing is the startup routine at start

    as you can see, adding ienumerator, there is a pause because the check closes,as you can see on lines 12 and 14.
    so my verification can't go through the cases
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Sorry, you're not making any sense.
    Switch statements work just fine in a coroutine. There is nothing a coroutine does that would change how a switch behaves.

    Line 12 is a closing bracket. Line 14 is your coroutine start. So I don't know what you are trying to do. Right now your coroutine isn't even being run. You don't have a StartCoroutine call in what you showed.

    If you want to call your coroutine, you have to use StartCoroutine and pass in the index value. That is why I asked you to show your code since you don't appear to be doing that anywhere.
     
  5. carnag45

    carnag45

    Joined:
    May 22, 2019
    Posts:
    25
    I also thought I was right
    I removed the ienumerator index and added a debug to each one, try to see and see that the thing is different.

    add for each button the previous and next function
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Ok, so right now your coroutine runs once in Start. If you want it to run after you change it's value, you need to call it again in your voidindex method.