Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Hecan · Feb 19, 2017 at 09:06 PM · coroutinewaitforsecondshealth

WaitForSeconds. Completely lost with this

Hello, I'm a complete beginner and i'm having some problems with my code. I have a text on a canvas, on which I'm trying to put a timer. The problem, I'm using a coroutine to delay my code, but somehow it's not doing what I wanted it to do. Can you help me?

 public class Alberto : MonoBehaviour {
 
    public void contador(int i)
     {
         while (i <= 100)
             {
             gameObject.GetComponent<Text>().text = i.ToString();
             Debug.Log(i);
             i++;
             StartCoroutine(Wait());
         }
     }
     IEnumerator Wait()
     {
         yield return new WaitForSeconds(1);
     }
 
 }
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
1
Best Answer

Answer by oStaiko · Feb 19, 2017 at 10:13 PM

You didn't say what you wanted this to do, so I'll just assume you want it to display the time as an int? The problem here, is that CoRoutines are weird and don't work that way. It takes some time to get used to them, but you can't just throw a Coroutine somewhere and have it act like a "pause" as you are trying to do here. The thing about coroutines is that when you call StartCoroutine, absolutely nothing happens to the main block of code. What IS happening, is that you started a NEW block of code, and both of these run at the same time. If you tell the new block to wait for a second, it doesn't make the first block wait, which is what you are trying to do. Tp achieve what you want here with code, this is what you'd need to do:

 public void contador (int i)
 {
     StartCoroutine(Wait(i));
 }
 
 IEnumerator Wait (int i)
 {
     while (i <= 100)
     {
         gameObject.GetComponent<Text>().text = i.ToString();
         Debug.Log(i);
         i++
         yield return new WaitForSeconds(1);
     }
 }

So as you can see here in this example, you're starting your Coroutine and it is handling the timer and the WaitForSeconds. The most important part you need to understand here, is that when you start a Coroutine, it's like you're having two functions run at the same time, and the WaitForSeconds only makes the Coroutine wait, not the main code. If you made the main code wait, your ENTIRE GAME would wait for a whole second, Update() is run once every frame. If you made Update wait, there'd be no new frames until it finished.

Now that that's out of the way, I'd like to recommend a few changes to your code, as it's pretty inefficient. The biggest thing here, is that GetComponent is very costly to call, and you want to do it as little as possible. Something like that should be put in a start () function if possible. Also for your timet, there is already a built in timer that can be accessed by Time.time, meaning you don't need a Coroutine for this. Here's a more polished version that does what you need it to:

 public class Alberto : MonoBehaviour
 {
     Text txt;
     float startTime;
     bool doTimer;
 
     public void Start ()
     {
         txt = gameObject.GetComponent<Text> ();
         StartTimer ();
     }
 
     public void StartTimer ()
     {
         startTime = Time.time;
         doTimer = true;
     }
 
     public void StopTimer ()
     {
         doTimer = false;
     }
 
     void Update ()
     {
         if (doTimer)
             txt.text = Mathf.RoundToInt (Time.time-startTime)+"s";
     }
 }

To start the timer, just call StartTimer (). For now I put that in Start () so the timer is active as soon as the scene is loaded. If you need to stop it, just call StopTimer ().

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

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

97 People are following this question.

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

Related Questions

Am I using this Coroutine and IEnumerator correctly? 1 Answer

Coroutine not waiting for yield WaitForSeconds [JS] 1 Answer

Problem with coroutine / create a gap of 5 seconds [C#] 1 Answer

Trying to make my code pause using coroutines, what am i doing wrong? 2 Answers

Performance Issue with Coroutines 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