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. Dismiss Notice

Error Making IENumaterator

Discussion in 'Scripting' started by Nigey, Apr 24, 2014.

  1. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Hi Guys,

    Trying to make an object half opacity, then slowly come back over time. I've made an IENumerator to handle the event, but it's coming back with a strange result.

    Code (csharp):
    1.  
    2.     IEnumerator SuperPill()
    3.     {
    4.         bSuperPill = true;
    5.         GameObject[] leukemias = GameObject.FindGameObjectsWithTag("Leukemia");
    6.  
    7.         // Start
    8.         for(int i = 0; i < leukemias.Length; i++)
    9.         {
    10.             if(leukemias[i] != null)
    11.             {
    12.                 Color colorEffect = leukemias[i].renderer.material.color;
    13.                 colorEffect.a = 0.5f;
    14.  
    15.                 leukemias[i].GetComponent<LeukemiaAI>().Move_Speed = 0.0265f;
    16.                 leukemias[i].renderer.material.color = colorEffect;
    17.             }
    18.         }
    19.  
    20.         yield return new WaitForSeconds( (fSuperPillTime - 0.5f));
    21.  
    22.         // Warn of end
    23.         leukemias = GameObject.FindGameObjectsWithTag("Leukemia");
    24.  
    25.         for(float t = 0.5f; t < 1; t += Time.deltaTime)
    26.         {
    27.             for(int i = 0; i < leukemias.Length; i++)
    28.             {
    29.                 Color colorEffect = leukemias[i].renderer.material.color;
    30.                 colorEffect.a = t;
    31.  
    32.                 Debug.Log(t + " _ " + colorEffect + " _ " + leukemias[i].renderer.material.color);
    33.  
    34.                 leukemias[i].renderer.material.color = colorEffect;
    35.             }
    36.         }
    37.  
    38.         // End
    39.         leukemias = GameObject.FindGameObjectsWithTag("Leukemia");
    40.  
    41.         for(int i = 0; i < leukemias.Length; i++)
    42.         {
    43.             if(leukemias[i] != null)
    44.             {
    45.                 Color colorEffect = leukemias[i].renderer.material.color;
    46.                 colorEffect.a = 1;
    47.  
    48.                 leukemias[i].GetComponent<LeukemiaAI>().Move_Speed = 0.053f;
    49.                 leukemias[i].renderer.material.color = colorEffect;
    50.             }
    51.         }
    52.  
    53.         bSuperPill = false;
    54.     }
    55.  
    Basically they go to half opacity. Then ignore the actions of the second For statement (and the one nested inside that), and go straight to full opacity.

    I've brought back the logs that SAY it's working, but the funny thing is the logs only appear in the Console after the entire event has finished. They will not appear until the entire EINumerator has ended.


    Code (csharp):
    1.  
    2. 0.9307263 _ RGBA(1.000, 1.000, 1.000, 0.931) _ RGBA(1.000, 1.000, 1.000, 0.914)
    3. UnityEngine.Debug:Log(Object)
    4. <SuperPill>c__Iterator2:MoveNext() (at Assets/Code/GameInformation.cs:132)
    5.  
    6. 0.9472927 _ RGBA(1.000, 1.000, 1.000, 0.947) _ RGBA(1.000, 1.000, 1.000, 0.931)
    7. UnityEngine.Debug:Log(Object)
    8. <SuperPill>c__Iterator2:MoveNext() (at Assets/Code/GameInformation.cs:132)
    9.  
    10. 0.9638591 _ RGBA(1.000, 1.000, 1.000, 0.964) _ RGBA(1.000, 1.000, 1.000, 0.947)
    11. UnityEngine.Debug:Log(Object)
    12. <SuperPill>c__Iterator2:MoveNext() (at Assets/Code/GameInformation.cs:132)
    13.  
    14. 0.9804255 _ RGBA(1.000, 1.000, 1.000, 0.980) _ RGBA(1.000, 1.000, 1.000, 0.964)
    15. UnityEngine.Debug:Log(Object)
    16. <SuperPill>c__Iterator2:MoveNext() (at Assets/Code/GameInformation.cs:132)
    17.  
    18. 0.9969919 _ RGBA(1.000, 1.000, 1.000, 0.997) _ RGBA(1.000, 1.000, 1.000, 0.980)
    19. UnityEngine.Debug:Log(Object)
    20. <SuperPill>c__Iterator2:MoveNext() (at Assets/Code/GameInformation.cs:132)
    21.  
    22.  
    Any ideas?

    Thanks guys
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    You're not yielding anywhere after you set the opacity to 50%. So, it is happening. Just in a single frame.
     
  3. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Awesome, thanks