Search Unity

Unity UI Having trouble with fading...

Discussion in 'UGUI & TextMesh Pro' started by Mystic_Quest, Jul 26, 2018.

  1. Mystic_Quest

    Mystic_Quest

    Joined:
    Feb 22, 2016
    Posts:
    47
    I am frustrated. Made a random generated dungeon and other intermediate level stuff and I can't fade in an image. Been searching for hours, trying things that didn't work. This is my current version of the script. I initially used only crossfadealpha which doesn't seem to work in accordance to what the documentation says. The canvasrenderer doesn't have a fade function according to vs and after reading that many people set its alpha before using crossfade for the image, I tried it but it didn't work either. Any help? (some gameobject names are the same with some variables - code also gets in the if statement)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Events : MonoBehaviour {
    7.  
    8.     public GameObject enemy;
    9.  
    10.     public GameObject win1;
    11.     public GameObject text1;
    12.  
    13.     public Image win;
    14.     public Text text;
    15.  
    16.     // Use this for initialization
    17.     void Start ()
    18.     {
    19.    
    20.  
    21.         win1 = GameObject.Find("win");
    22.         text1 = GameObject.Find("text");
    23.  
    24.         win1.GetComponent<CanvasRenderer>().SetAlpha(0f);
    25.         text1.GetComponent<CanvasRenderer>().SetAlpha(0f);
    26.  
    27.         win = win1.GetComponent<Image>();
    28.         text = text1.GetComponent<Text>();
    29.  
    30.         enemy = GameObject.FindGameObjectWithTag("enemy");
    31.  
    32.      
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void Update ()
    37.     {
    38.  
    39.         if (enemy == null)
    40.         {
    41.  
    42.             //win.SetActive(true);
    43.  
    44.             win.CrossFadeAlpha(1f, 2f, false);
    45.             text.CrossFadeAlpha(1f, 2f, false);
    46.  
    47.    
    48.         }
    49.     }
    50. }
     
  2. Mystic_Quest

    Mystic_Quest

    Joined:
    Feb 22, 2016
    Posts:
    47
    The solution was as simple as having 1 instead of 0 in the inspector options of the image/text color alpha. It won't increase if it's 0 for some reason but it will if it's 1 and with "win.CrossFadeAlpha(255f, 2f, false)".