Search Unity

Question How can I fade a sprite renderer in and out on loop?

Discussion in 'Scripting' started by jleven22, Dec 16, 2022.

  1. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    421
    I've written it all out, but maybe I've just been starting at it too long. Either way, the loop is never looping, so I'm clearly doing something wrong:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Whirlpool : MonoBehaviour
    6. {
    7.     public SpriteRenderer theSR;
    8.     private float waitCountdownIn, waitCountdownOut;
    9.     private bool isFadingIn, isFadingOut;
    10.  
    11.  
    12.     void Start()
    13.     {
    14.         waitCountdownIn = 3f;
    15.         waitCountdownOut = 0f;
    16.     }
    17.  
    18.      
    19.     void Update()
    20.     {
    21.         if (waitCountdownIn > 0)
    22.         {
    23.  
    24.             waitCountdownIn -= Time.deltaTime;
    25.         }
    26.         else
    27.         {
    28.             if (!isFadingIn)
    29.             {
    30.                 StartCoroutine(FadeIn());
    31.             }
    32.         }
    33.  
    34.         if (waitCountdownOut > 0)
    35.         {
    36.  
    37.             waitCountdownOut -= Time.deltaTime;
    38.         }
    39.         else
    40.         {
    41.             if (!isFadingOut)
    42.             {
    43.                 StartCoroutine(FadeOut());
    44.             }
    45.         }
    46.  
    47.         print("waitCountdownIn = " + waitCountdownIn);
    48.         print("waitCountdownOut = " + waitCountdownOut);
    49.         print("isFadingIn = " + isFadingIn);
    50.         print("isFadingOut = " + isFadingOut);
    51.  
    52.  
    53.  
    54.  
    55.     }
    56.  
    57.     private IEnumerator FadeIn()
    58.     {
    59.         isFadingIn = true;
    60.  
    61.         float alphaVal = theSR.color.a;
    62.         Color tmp = theSR.color;
    63.  
    64.         while (theSR.color.a > 0)
    65.         {
    66.             alphaVal -= 0.01f;
    67.             tmp.a = alphaVal;
    68.             theSR.color = tmp;
    69.  
    70.             yield return new WaitForSeconds(0.05f); // update interval
    71.         }
    72.  
    73.         yield return new WaitForSeconds(3f); // update interval
    74.         isFadingIn = false;
    75.         waitCountdownOut = 3f;
    76.     }
    77.  
    78.     private IEnumerator FadeOut()
    79.     {
    80.         isFadingOut = true;
    81.  
    82.         float alphaVal = theSR.color.a;
    83.         Color tmp = theSR.color;
    84.  
    85.         while (theSR.color.a < 1)
    86.         {
    87.             alphaVal += 0.01f;
    88.             tmp.a = alphaVal;
    89.             theSR.color = tmp;
    90.  
    91.             yield return new WaitForSeconds(0.05f); // update interval
    92.         }
    93.  
    94.         yield return new WaitForSeconds(3f); // update interval
    95.         isFadingOut = false;
    96.         waitCountdownIn = 3f;
    97.  
    98.     }
    99. }
    100.  
    The debug shows "isFadingOut = true" permanently. I'm just getting confused as to why.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
  3. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    421
    Took your advice and did it as an animation instead. Then added the functions to the animation timeline. Thanks!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Whirlpool : MonoBehaviour
    6. {
    7.     public Animator anim;
    8.     private float isOnCountDown;
    9.     private float isOffCountDown;
    10.     public
    11.  
    12.  
    13.  
    14.      
    15.     void Update()
    16.     {
    17.         if (isOnCountDown > 0)
    18.         {
    19.  
    20.             isOnCountDown -= Time.deltaTime;
    21.         }
    22.         else
    23.         {
    24.             WhirlpoolOutro();
    25.         }
    26.  
    27.         if (isOffCountDown > 0)
    28.         {
    29.  
    30.             isOffCountDown -= Time.deltaTime;
    31.         }
    32.         else
    33.         {
    34.             WhirlpoolIntro();
    35.         }
    36.  
    37.     }
    38.  
    39.     //intro animation start
    40.     //whirlpool animation start & turn on children
    41.     //outro animation start
    42.     //idle start
    43.  
    44.     public void WhirlpoolIntro()
    45.     {
    46.         anim.SetBool("Off", false);
    47.         anim.SetTrigger("Intro");
    48.         anim.SetBool("Intro", true);
    49.  
    50.     }
    51.  
    52.     public void WhirlpoolOn()
    53.     {
    54.         anim.SetBool("Intro", false);
    55.         anim.SetBool("On", true);
    56.         isOnCountDown = Random.Range(4, 7);
    57.     }
    58.  
    59.     public void WhirlpoolOutro()
    60.     {
    61.         anim.SetBool("On", false);
    62.         anim.SetTrigger("Outro");
    63.  
    64.     }
    65.  
    66.     public void WhirlpoolOff()
    67.     {
    68.         anim.SetBool("Off", true);
    69.         isOffCountDown = Random.Range(5, 10);
    70.  
    71.     }
    72.  
    73. }
    74.