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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Make text appear then disappear

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

  1. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    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:
    9,996
    yes.
     
    unity_n_rN95v2cIEGlQ likes this.
  3. warrenbrandt

    warrenbrandt

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

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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:
    413
    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:
    413
    this was awesome

     
    JesseTWMorgan likes this.
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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:
    13
    Where do I put my text and or my text name in UI I can't figure that out for some reason.
     
    majas_17 likes this.
  9. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.
     
  10. n1llaurado

    n1llaurado

    Joined:
    Oct 22, 2020
    Posts:
    2
    Very cool, Thanks.
     
    Joe-Censored likes this.
  11. AshwinTheGammer

    AshwinTheGammer

    Joined:
    Sep 23, 2016
    Posts:
    68
    If you'd have to move the text up the screen, how would you do that?
    Would you do that using Animation (Animating UI Text) or some sort of script?

    Actually, I'm trying to find how to move the text up the screen while lowering it's Colors alpha value.
     
    Last edited: Jun 4, 2021
  12. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I think I was talking about just changing the text object's position every frame.
     
    AshwinTheGammer likes this.
  13. kj231053

    kj231053

    Joined:
    Aug 30, 2021
    Posts:
    3
    How do I need to reference Text on line one in order for unity to know what the code is addressing?
    Is there a certain script I need to write in order to declare Text?
     
  14. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Text in this example refers to a Text component. You'd have the Text component attached to a GameObject, and set the reference for this script to it by just dragging it over in the Inspector.

    Unity moved all the UI stuff into its own package for Unity versions from the past couple years, which is why UI.Text is absent from the official scripting reference unless you go back to somewhere around 2017.x versions. I believe this is the package documentation though:

    https://docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/UIVisualComponents.html
     
  15. kj231053

    kj231053

    Joined:
    Aug 30, 2021
    Posts:
    3
    Alright I'll try it out real quick!
     
  16. kj231053

    kj231053

    Joined:
    Aug 30, 2021
    Posts:
    3
    I tried doing what you suggested and I'm still getting a compile error.
     

    Attached Files:

  17. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    The portion of the script I posted isn't a complete script. You'll need the UI package installed, and you'll need the following line at the top of the script, in addition to all the other stuff you need to make a complete script.

    Code (csharp):
    1. using UnityEngine.UI;
     
  18. Honorsoft

    Honorsoft

    Joined:
    Oct 31, 2016
    Posts:
    81
    For anyone still looking, Invoke is another easy way to set a timer to make text disappear:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. public class CanvasScript : MonoBehaviour
    5. {
    6. public Text canvasText1;
    7.    void Start ()
    8.    {
    9.       Invoke("DisableText", 5f);//invoke after 5 seconds
    10.    }
    11.    void DisableText()
    12.    {
    13.       canvasText1.enabled = false;
    14.    }
    15. }
    16.  
    (Work smart not hard)
     
    Last edited: Apr 28, 2022
    Nekocat284 likes this.