Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Nition · Jul 31, 2012 at 12:07 AM · timercoroutines

Best way to implement a one-shot timer

I have a situation where I have a method that will update a text field and show it, and that text stays on screen for x amount of time - say 5 seconds - then it automatically goes away again.

I know I can do that easily with a coroutine that starts up and fires after 5 seconds. The problem with that is that the text might be updated after, say, 2 seconds. The original coroutine would still fire and hide the text field too early - after 3 more seconds. It seems clunky to manually stop any running coroutines to fix that situation.

I used to use a language where you'd do it with a single oneshot timer, like this (pseudocode):

 OneShotTimer myTimer {
   .interval = 5000;

   void OnTimerFires() {
     HideText();
     // OneShotTimer automatically stops; otherwise call .stop()
   }
 }

 void ShowText(string text) {
   myText.text = text;
   myText.show()
   myTimer.start()
 }

If the timer was .start()-ed when it was already running, it'd reset, so the text would show for the correct time.

So, this is probably a dumb question, but I'm sure others must have had this situation before. What's the best way to do this in Unity/C#? A good way of doing it with coroutines? I know there's also a C# timer class - would that be better to use? Of course I could also check the time elapsed since ShowText was last called in the Update() method but I know that's much worse for performance. Thanks for any suggestions.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Avaista · Jul 31, 2012 at 02:01 AM

This would work in terms of simplicity, though it requires the retention of a timer and boolean value. Honestly it's not utterly terrible either way, unless you have this stuff everywhere.

  public float killTime;
  public bool showingText;
  public float textTime;

  IEnumerator ShowText()
  {
       killTime = Time.time + textTime;
       if(!showingText)
       {
            showingText = true;
            ShowText();
            while(Time.time < killTime)
                 yield return null;// or yield return new WaitForSeconds(killTime-Time.time);
            KillText();
            showingText = false;
        }
        yield break;
   }
  
Comment
Add comment · Show 1 · 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
avatar image Nition · Aug 01, 2012 at 10:34 PM 0
Share

Good suggestion, thanks.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Coroutine Countdown Timer being extremely slow 1 Answer

Can't get 2d bomb to explode on timer 2 Answers

Accuracy of timer issue please help 2 Answers

how do I make a Time Based Score System? 1 Answer

Unity ignoring if statement for high score 2 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