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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Coroutine throws "index out of range" at last curly bracket

Discussion in 'Scripting' started by calpolican, Sep 4, 2020.

  1. calpolican

    calpolican

    Joined:
    Feb 2, 2015
    Posts:
    400
    So, I have a coroutine that throws an index out of range exception. I know what this exception means, but my code throws it at the last line of the coroutine. The only thing in that line is the "}" that closes the coroutine.
    Any clue? Could it be that it's expecting to keep iterating the IEnumerator and calls MoveNext() but finds nothing because I've stopped the coroutine from somewhere else? How can I solve it?

    Code (CSharp):
    1.   public IEnumerator ActivarCooldown(bool ataque = false, float inicio = 0)
    2.     {
    3.  
    4.         #region Almacenando el cooldown para que no tenga que estar buscándolo mil veces
    5.  
    6.         Image eCooldown = null;
    7.         Scrollbar rCooldown = null;
    8.  
    9.         if (this is Robert rob) { rCooldown = rob.PoolManager.pooledActions[0].progressScroll; }
    10.         else { eCooldown = ((BaseEnemy) this).retrato.cooldown; }
    11.  
    12.         #endregion
    13.  
    14.         #region > ENEMY
    15.  
    16.         //Esta intenta que no puedan tirar cuando están corriendo. <esto no está bien acá.>
    17.         if (Moving && !isRobert)
    18.         {
    19.  
    20.             //Reseteamos el cooldown a 1.
    21.             eCooldown.fillAmount = 1;
    22.  
    23.             yield return null;
    24.         }
    25.  
    26.         #endregion
    27.  
    28.         #region ~ RELOJ ~
    29.  
    30.  
    31.         #region Definiciones del reloj
    32.  
    33.  
    34.         //Este es el tiempo que va a tardar.
    35.         float duracion = 10 - Stats.Agility; if (isRobert) { duracion /= 2; }
    36.  
    37.  
    38.         Debug.Log ($"{CharacterName} - cooldown duración = {duracion}");
    39.  
    40.         //Esta sirve para aumentar el cooldown si pasa algo, x ej acelerarlo si el personaje no está cubierto
    41.         modificadorCooldown = 1;
    42.  
    43.         //El contador es lo que vamos a usar para ir midiendo el tiempo que pasa. Es igual a inicio, porque inicio es casi siempre cero, salvo que quieras que empiece con una parte ya transcurrida (en la persistencia x ej.).
    44.         float contador = duracion * inicio;
    45.  
    46.         //Este es el reloj porpiamente dicho.
    47.         float reloj = 1 - contador / duracion * modificadorCooldown;
    48.  
    49.  
    50.         #endregion
    51.  
    52.         while (reloj > 0)
    53.         {
    54.  
    55.             //Vamos agregando al timer el tiempo que corresponde (usamos delta time para que no haya problemas)
    56.             contador += Time.deltaTime;
    57.  
    58.             //Vamos subiendo la barra del relojito para que el jugador pueda saber por donde va.
    59.             reloj = 1 - (contador / duracion * modificadorCooldown);
    60.  
    61.  
    62.             //Debug.LogWarning ($"{CharacterName} cooldown reloj = {reloj}, contador {contador}, modificador {modificadorCooldown} - la conidición es que el reloj sea mayor a cero, contador / duracion * modificadorCooldown = {reloj +1}");
    63.  
    64.             if (isRobert) { rCooldown.size = reloj; } else { eCooldown.fillAmount = reloj; }
    65.  
    66.             yield return null;
    67.         }
    68.  
    69.         //Al final, poné el cooldown en cero.
    70.         if (isRobert) { rCooldown.size = 0; } else { eCooldown.fillAmount = 0; }
    71.  
    72.  
    73.         #endregion
    74.  
    75.         #region ROBERT <
    76.  
    77.         //Si sos Robert, ejecutá la acción
    78.         if (isRobert) { DoAction (); }
    79.  
    80.         #endregion
    81.  
    82.     } //<-------------- the error in the console blames this line.
    83.  
     
    Last edited: Sep 4, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,779
    Based on that either the code above has not compiled (possibly due to an error) and you are running an earlier compilation of it, or else the code is unable to identify where the failure is happening.

    To fix it, every place you do an array dereference (from a list or an array), print the index value, and print the Length (or Count) of the collection, and you will soon see where the problem is. Here is some more things to think about:

    http://plbm.com/?p=236
     
  3. calpolican

    calpolican

    Joined:
    Feb 2, 2015
    Posts:
    400
    I have saved the code in VS and compiled. I've added new lines to verify that the line shown is indeed the curly bracket. So, it's not that.
     
  4. Nexade

    Nexade

    Joined:
    Feb 29, 2020
    Posts:
    2
    I have the same problem where there's no errors in the code (I've checked and double checked) but Unity still calls an error on the last bracket of the Coroutine. If you've found a solution can you let me know
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,917
    Make your own thread and post your code (in code tags), rather than necro a 2 year old post.
     
    Bunny83 likes this.