Search Unity

Make text appear then disappear

Discussion in 'Getting Started' started by warrenbrandt, Jul 25, 2019.

  1. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    110
    How can I have text appear for a few seconds on the screen then disappear?
    does it have to be done through a script, or does the engine handle it?
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    4,266
    yes.
     
  3. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    110
    wow, very elaborate answer
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    6,268
    This probably works. Haven't tested; just wrote straight into the forum post.

    Code (csharp):
    1. public Text WhateverTextThingy;  //Add reference to UI Text here via the inspector
    2. private float timeToAppear = 2f;
    3. private float timeWhenDisappear;
    4.  
    5. //Call to enable the text, which also sets the timer
    6. public void EnableText()
    7. {
    8.     WhateverTextThingy.enabled = true;
    9.     timeWhenDisappear = Time.time + timeToAppear;
    10. }
    11.  
    12. //We check every frame if the timer has expired and the text should disappear
    13. void Update()
    14. {
    15.     if (WhateverTextThingy.enabled && (Time.time >= timeWhenDisappear))
    16.     {
    17.         WhateverTextThingy.enabled = false;
    18.     }
    19. }
    Though when I do this kind of thing in my own games, I usually go more elaborate. Such as moving the text up the screen while I lower the text color's alpha to have it fade out instead of disappear. And I have some manager doing all this for multiple text objects, and flow control of messages so they aren't displayed on top of each other. Not exactly sure what you are going for though.
     
  5. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    110
    Thanks a bunch Joe!!! Wow, that is amazing, wish I could just program out of my head like you do.

    does that script go on the Text object or the canvas?

    very new here, look a fade in and out would be great, and a management system (i have one for my audio) would be great, but im not sure how, looked on youtube, no tutorials.
     
    Last edited: Jul 26, 2019
  6. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    110
    this was awesome

     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    6,268
    The short script I posted just needs to be on some active gameobject with a reference to the Text component. It could be on the same object as the Text component.
     
  8. AlexJohns

    AlexJohns

    Joined:
    Aug 23, 2019
    Posts:
    3
    Where do I put my text and or my text name in UI I can't figure that out for some reason.
     
  9. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    6,268
    I'm not exactly sure what you're asking. A UI Text is a component you put on a GameObject. See any basic UI tutorial for dealing with the placement of these objects.