• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Sfullen96 · Mar 31, 2015 at 09:42 AM · coroutinecoroutines

How to make UI text appear on screen for a limited time then disappear

If you've played Skyrim or a similar game, when you pick up a bag of gold it will come up on screen saying how much gold was in said bag, I am trying to achieve this.

Basically when the player presses E when the raycast is looking at the bag it will give the player a random amount of gold, my problem is I can't get the text that says "you collected 'x' amount of gold" to disappear from the screen after a few seconds, I have tried using coroutines but I don't fully understand them despite watching Unity videos on it and looking at the docs.

Any help would be greatly appreciated, I think once I understand coroutines better I will realise that I've made some stupid mistake or completely misunderstood the concept of coroutines. I am still quite new to unity so don't flame me please :)

here is what I have so far:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 namespace UnityStandardAssets.Characters.FirstPerson
 {
     public class GoldBagController : MonoBehaviour, InteractableObject
     {
         public GameObject goldBag;
         public PlayerController player;
         public CharUI charUI;
         public float bagAmount;
         public bool isGoldCollected;
 
         void Start()
         {
             isGoldCollected = false;
             player.goldCount = 0;
             //charUI.goldAmount.text = "You got " + (int)bagAmount + " gold!";
         }
 
         void Update()
         {
             bagAmount = Random.Range(20f, 5000f);
         }
 
 
         IEnumerator GoldAmountTip(string message, float delay)
         {
 
             if (isGoldCollected == true) //when gold bag is collected add the gold to gold amount, remove the gold bag and print a message
             {
                 player.goldCount += (int)bagAmount;
                 charUI.hoverTip.text = "";
                 Destroy(goldBag);
                 charUI.goldAmount.text = message;
             }
 
             yield return new WaitForSeconds(delay); //delay for 3 seconds then set text to nothing, this is where I am having trouble
             charUI.goldAmount.text = "";
         }
 
         /**
          * Interact method is called when player presses E when its raycast is looking at the gold bag
          **/
         public void Interact()
         {
             isGoldCollected = true; 
 
             StartCoroutine(GoldAmountTip("You got " + (int)bagAmount + " gold!", 3));
         }
     }
 }
Comment

People who like this

0 Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlwaysSunny · Mar 31, 2015 at 05:50 AM 0
Share

I don't spy anything wrong here. The isGoldCollected bool is pointless, and the bagAmount randomization should happen on Interact().

Message display systems are typically more unified, resulting in far less redundant code; perhaps a dynamic collection of instantiated UI objects you request from a ScreenMessage singleton. The singleton would handle every on-screen pop-up message like this, and do so with a single call from any script:

 ScreenMessage.Print(message, time, color, priority); // etc...

That said, unless you're stopping this coroutine, destroying this object, or changing / losing the reference to the charUI.goldAmount object, this should result in a 3-second display of the message.

1 Reply

  • Sort: 
avatar image

Answer by tomhughes · Mar 31, 2015 at 09:55 AM

Add a public int

public int delay=3;

then after the "waitforseconds" set gui/text to .enabled=false?

would that work?

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Flash image while ammo is low - coroutines? 3 Answers

Instantiated object from asset bundle is null 0 Answers

Yield return null? 3 Answers

Waiting for and using output of a UnityEvent within coroutines 1 Answer

Question about Coroutine Execution Order 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges