Search Unity

How should I Fade a panel?

Discussion in 'Getting Started' started by sinae Kim, Jun 1, 2015.

  1. sinae Kim

    sinae Kim

    Joined:
    Jun 1, 2015
    Posts:
    1
    usingUnityEngine;
    usingSystem.Collections;

    publicclassFade : MonoBehaviour
    {
    publicUnityEngine.UI.Image Panel;


    voidStart ()
    {
    Panel = GetComponent<UnityEngine.UI.Image> ();
    }

    voidUpdate ()
    {
    Panel.CrossFadeColor (Color.black, 2.0f, false, true);

    }


    }

    When I'm running this code.... there is error MSG Like this "
    NullReferenceException: Object reference not set to an instance of an object
    Fade.Update () (at Assets/Script/Fade.cs:16)"

    I already Put a UI Panel object.. what's wrong??
     
  2. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
    This script is attached to a GameObject with an Image? Must not be?
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
  4. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,052
    NomadKing likes this.
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Thanks! I know you have strict standards on random YouTube tutorials. Glad I measure up.
     
    blizzy likes this.
  6. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,052
    I put yours on my 'approved' list at work. ;) I hope you continue your ones on general concepts.
     
    Kiwasi likes this.
  7. Divyansh_K

    Divyansh_K

    Joined:
    Jul 25, 2017
    Posts:
    4
    hi, just use this script i created , it looks complex but it is actually highly optimized , use show and hide functions to fade in and fade out, have fun : )


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class UI_Fader : MonoBehaviour {

    public bool startAsHidden;
    public float fadeSpeed = 5f;

    List<Color> imageInitialColors;
    bool hide = false,fullyOptimised;
    int OptimisationInt = 0;


    public void Hide()
    {
    if(OptimisationInt != -1)
    {
    hide = true;
    setUnOptimised ();
    }
    }

    public void Show()
    {
    if(OptimisationInt != 1)
    {
    hide = false;
    setUnOptimised ();
    }
    }

    void setUnOptimised()
    {
    fullyOptimised = false;
    OptimisationInt = 0;
    }

    void Awake()
    {
    imageInitialColors = new List<Color> ();
    foreach(Image image in GetComponentsInChildren <Image>())
    {
    imageInitialColors.Add (image.color);
    }
    foreach(Text txt in GetComponentsInChildren <Text>())
    {
    imageInitialColors.Add (txt.color);
    }

    if(startAsHidden)
    {
    foreach(Image image in GetComponentsInChildren <Image>())
    {
    image.color = new Color (image.color.r, image.color.g, image.color.b, 0);
    }
    foreach(Text txt in GetComponentsInChildren <Text>())
    {
    txt.color = new Color (txt.color.r, txt.color.g, txt.color.b, 0);
    }

    OptimisationInt = -1;
    fullyOptimised = true;
    }
    }


    void Update()
    {
    if(!fullyOptimised)
    {
    if(OptimisationInt == 0)
    {
    if(hide)
    {
    foreach(Image image in GetComponentsInChildren <Image>())
    {
    image.color = Color.Lerp (image.color, new Color (image.color.r, image.color.g, image.color.b, 0), fadeSpeed * Time.deltaTime);
    }

    foreach(Text txt in GetComponentsInChildren <Text>())
    {
    txt.color = Color.Lerp (txt.color, new Color (txt.color.r, txt.color.g, txt.color.b, 0), fadeSpeed * Time.deltaTime);

    if (txt.color.a < 0.05f)
    OptimisationInt = -1;
    }
    }
    else
    {
    int i = 0;
    foreach(Image image in GetComponentsInChildren <Image>())
    {
    image.color = Color.Lerp (image.color, imageInitialColors, fadeSpeed * Time.deltaTime);
    i++;
    }
    foreach(Text txt in GetComponentsInChildren <Text>())
    {
    txt.color = Color.Lerp (txt.color, imageInitialColors, fadeSpeed * Time.deltaTime);
    i++;

    if (txt.color.a > 0.95f)
    OptimisationInt = 1;
    }
    }
    }
    else
    {
    if(OptimisationInt == 1)
    {
    int i = 0;
    foreach(Image image in GetComponentsInChildren <Image>())
    {
    image.color = imageInitialColors ;
    i++;
    }
    foreach(Text txt in GetComponentsInChildren <Text>())
    {
    txt.color = imageInitialColors;
    i++;
    }
    }
    else if(OptimisationInt == -1)
    {
    foreach(Image image in GetComponentsInChildren <Image>())
    {
    image.color = new Color (image.color.r, image.color.g, image.color.b, 0);
    }

    foreach(Text txt in GetComponentsInChildren <Text>())
    {
    txt.color = new Color (txt.color.r, txt.color.g, txt.color.b, 0);
    }
    }

    fullyOptimised = true;
    }
    }

    }

    }
     
  8. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,052
    That is really pretty heavy, not sure how it could be called optimized. You are not caching, and along multiple foreach loops of getcomponents on stacks of children, lerping the whole color instead of just the alpha. I'm not sure ther is a less optimal way to do it.

    Just use canvas group.
     
    Kiwasi likes this.
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    In what world is calling GetComponent in Update highly optimised?
     
    Issun, Pixel_Sized and Rafarel like this.