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

Simple Trigger question C#

Discussion in 'Scripting' started by Broncoman, Dec 2, 2015.

  1. Broncoman

    Broncoman

    Joined:
    Aug 13, 2015
    Posts:
    3
    I have an environment where, when a user approaches a specific object, a UI "fades in" above it. The trigger is working, and I can get the text header to fade in, but getting the entire UI panel (including the buttons contained within it) to fade in has me stumped.
    I've looked at a myriad of tutorials, Youtube videos, forum posts, etc., but cannot seem to find the missing component. I'm certain it's something simple, but I'm not an engineer and don't write code, so this is all new to me.
    Can one of you expert-type guys look at my code and tell me what I'm missing? This is where I'm at so far:

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

    public class fadeEffects : MonoBehaviour {

    public Text headerText;
    public float fadeSpeed = 5f;
    public bool entrance;
    public GameObject Canvas;

    void Awake ()
    {
    headerText = Canvas.GetComponentInChildren<Text> ();
    headerText.color = Color.clear;

    }

    void Update ()
    {
    ColorChange();
    }

    void OnTriggerEnter (Collider col)
    {
    if (col.gameObject.tag == "Player")
    {
    entrance = true;
    }

    }

    void OnTriggerExit (Collider col)
    {
    if (col.gameObject.tag == "Player")
    {
    entrance = false;

    }

    }

    void ColorChange ()
    {

    if (entrance)
    {
    headerText.color = Color.Lerp (headerText.color, Color.white, fadeSpeed * Time.deltaTime);

    }

    if (!entrance)
    {
    headerText.color = Color.Lerp (headerText.color, Color.clear, fadeSpeed * Time.deltaTime);
    }

    }
    }
     

    Attached Files:

  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    Hi,

    See 17:35 of this video

    You need to use a CanvasGroup component. Change the alpha of that instead of the text.
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  4. Broncoman

    Broncoman

    Joined:
    Aug 13, 2015
    Posts:
    3
    Thanks, guys! I appreciate your feedback!
     
  5. michaelhill

    michaelhill

    Joined:
    Nov 4, 2015
    Posts:
    10
    I'd like to add that if you use the CrossFadeAlpha() function on a CanvasGroup, it will only work if your alpha value is non-zero. So if you want to fade from transparent to opaque, you'll want to set your alpha to something very small first.
     
  6. pkr1234

    pkr1234

    Joined:
    Nov 7, 2015
    Posts:
    2
    I want to help to write a c# script and support a shooting character with unity.and how many process doing easy way learning this process