Search Unity

How to make text within couroutine fade?

Discussion in 'Editor & General Support' started by IrelandMan, Mar 29, 2020.

  1. IrelandMan

    IrelandMan

    Joined:
    Sep 22, 2019
    Posts:
    4
    I would like to have the text fade away. How would one implement that into this script?


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;

    [ExecuteInEditMode]
    public class TurnBack : MonoBehaviour {

    [Space(10)]
    [Header("Toggle for the gui on off")]
    public bool GuiOn;


    [Space(10)]
    [Header("The text to Display on Trigger")]
    [Tooltip("To edit the look of the text Go to Assets > Create > GUIskin. Add the new Guiskin to the Custom Skin proptery. If you select the GUIskin in your project tab you can now adjust the Label section to change this text")]
    public string Text = "Turn Back";

    [Tooltip("This is the window Box's size. It will be mid screen. Add or reduce the X and Y to move the box in Pixels. ")]
    public Rect BoxSize = new Rect( 0, 0, 200, 100);


    [Space(10)]
    [Tooltip("To edit the look of the text Go to Assets > Create > GUIskin. Add the new Guiskin to the Custom Skin proptery. If you select the GUIskin in your project tab you can now adjust the font, colour, size etc of the text")]
    public GUISkin customSkin;



    // if this script is on an object with a collider display the Gui
    void OnTriggerEnter()
    {
    GuiOn = true;
    StartCoroutine("MyCoroutineName");
    }


    IEnumerator MyCoroutineName()
    {
    yield return new WaitForSeconds(3);
    GuiOn = false;
    }

    void OnGUI()
    {

    if (customSkin != null)
    {
    GUI.skin = customSkin;
    }

    if (GuiOn == true)
    {
    // Make a group on the center of the screen
    GUI.BeginGroup (new Rect ((Screen.width - BoxSize.width) / 2, (Screen.height - BoxSize.height) / 2, BoxSize.width, BoxSize.height));
    // All rectangles are now adjusted to the group. (0,0) is the topleft corner of the group.

    GUI.Label(BoxSize, Text);

    // End the group we started above. This is very important to remember!
    GUI.EndGroup ();

    }


    }

    }
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
    Olmi likes this.
  3. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    I'd like to add that it might be a good idea to remove extra "fluff" from the code like Tooltips etc. that really aren't essential for a question, they just add more lines that are really not important.