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

Change Background Image Breaks Fade

Discussion in 'Scripting' started by mholmes, Jan 25, 2020.

  1. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    407
    When I change the background image sprite, it breaks my code for fade in and fade out:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Fade_Background : MonoBehaviour
    7. {
    8.     public Image BackgroundImage;
    9.     public int _Delay;
    10.  
    11.     private bool _FadeOut = false;
    12.     private bool _Running = false;
    13.     private bool _rotate = false;
    14.     private int _Image = 0;
    15.  
    16.     private void Start()
    17.     {
    18.         Change_Image();
    19.         GameObject imageObject = GameObject.FindGameObjectWithTag("imgBackground");
    20.         BackgroundImage = imageObject.GetComponent<Image>();      
    21.     }
    22.  
    23.     void Update()
    24.     {
    25.         if (!_Running)
    26.         {
    27.             if (_FadeOut)
    28.             {
    29.                 StartCoroutine(FadeOut());
    30.                 if(_rotate) { Change_Image(); }
    31.             }
    32.             else
    33.             {
    34.                 StartCoroutine(FadeIn());
    35.             }
    36.         }
    37.     }
    38.  
    39.     private void Change_Image()
    40.     {
    41.         _rotate = false;      
    42.  
    43.         switch (_Image)
    44.         {
    45.             case 0:
    46.                 BackgroundImage.sprite = Image_Game_Data._Templo_Mayor;
    47.                 break;
    48.             case 1:
    49.                 BackgroundImage.sprite = Image_Game_Data._Pyramid_Of_The_Sun;
    50.                 break;
    51.             case 2:
    52.                 BackgroundImage.sprite = Image_Game_Data._El_Castillo;
    53.                 break;
    54.             case 3:
    55.                 BackgroundImage.sprite = Image_Game_Data._Dzibilchaltun;
    56.                 break;
    57.             case 4:
    58.                 BackgroundImage.sprite = Image_Game_Data._Kinich_Kakmo;
    59.                 break;
    60.             case 5:
    61.                 BackgroundImage.sprite = Image_Game_Data._Cenote_Xlacah;
    62.                 break;
    63.             case 6:
    64.                 BackgroundImage.sprite = Image_Game_Data._Ball_Court;
    65.                 break;
    66.         }
    67.  
    68.         if (_Image == 6) { _Image = 1; } else { _Image++; }
    69.     }
    70.  
    71.     IEnumerator FadeOut()
    72.     {
    73.         _Running = true;
    74.         if (BackgroundImage == null)
    75.         {
    76.             GameObject imageObject = GameObject.FindGameObjectWithTag("imgBackground");
    77.             BackgroundImage = imageObject.GetComponent<Image>();
    78.         }
    79.  
    80.         Color tmpColor = BackgroundImage.color;
    81.  
    82.         while (BackgroundImage.color.a > 0.0f)
    83.         {
    84.             tmpColor.a -= Time.deltaTime / 4;
    85.             BackgroundImage.color = tmpColor;
    86.             yield return null;
    87.         }
    88.  
    89.         //if (BackgroundImage.color.a < 0.0f) {  _rotate = true; }
    90.  
    91.         _FadeOut = false;
    92.         _Running = false;
    93.         yield return null;
    94.     }
    95.  
    96.     IEnumerator FadeIn()
    97.     {
    98.         _Running = true;
    99.         if (BackgroundImage == null)
    100.         {
    101.             GameObject imageObject = GameObject.FindGameObjectWithTag("imgBackground");
    102.             BackgroundImage = imageObject.GetComponent<Image>();
    103.         }
    104.        
    105.         Color tmpColor = BackgroundImage.color;
    106.      
    107.         while (BackgroundImage.color.a < 1.0f)
    108.         {
    109.             tmpColor.a += Time.deltaTime / 4;
    110.             BackgroundImage.color = tmpColor;
    111.             yield return null;
    112.         }
    113.  
    114.         yield return new WaitForSecondsRealtime(_Delay);
    115.  
    116.         _FadeOut = true;
    117.         _Running = false;
    118.         yield return null;
    119.     }
    120. }
    121.  
     
  2. Snipe76

    Snipe76

    Joined:
    May 23, 2017
    Posts:
    12
    A few points:
    1. What happens when the fadeout or fadein section is executed? you didn't explain anything.
    2. Your code has no comments in it (Makes me work for it to get it, which is a no no from me)
    3. Are there any errors or warnings messages?
    4. Videos / Screenshots help.

    Edit: a tip for the future, it's easier to use the animator to do the fade in / fade out stuff than code.
     
    Last edited: Jan 25, 2020
  3. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    407
    Hmm I think Animation might be better but I want to cycle through images. Can I do that with an animation still?