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 /
avatar image
0
Question by DissonanceMask · Sep 06, 2019 at 10:14 PM · coroutinetimercountdowndebug.log

Coroutine counts down to zero, but doesnt show "countdown has reached 0" on the exact moment the timer hits 0

Greetings, everyone reading this post!

I'm practicing making a coroutine countdown timer, and im pretty proud of my progress, but one thing baffles me: no matter where i put the "if (CountdownSeconds == 0)", it always posts the "Countdown has reached 0!" before the Debug.Log shows 0 on the countdown. and if I put it outside underneath the curly brackets, it takes a second until it shows "Countdown has reached 0!"

My goal is to get it to show "0", then "Countdown Timer has reached 0!" immediately, but after the countdown hits zero. Please show me the way, and explain how I could fix it!

just copy and paste my code! p.s. use the spacebar to StartCoroutine(); using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class CoroutineTest : MonoBehaviour
 {
     [SerializeField] float CountdownSeconds = 3f;
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         if(Input.GetKeyDown(KeyCode.Space))
         {
             StartCoroutine(CountdownTime());
         }
         else
         {
             StopCoroutine(CountdownTime());
         }
     }
 
     IEnumerator CountdownTime()
     {
         while(CountdownSeconds >= 0)
         {
             Debug.Log(CountdownSeconds);
             CountdownSeconds--;
             yield return new WaitForSeconds(1);
             if (CountdownSeconds <= 0)
             {
                 Debug.Log("Countdown has reached 0!");
             }
         }       
 
         StopCoroutine(CountdownTime());
         Debug.Log("Coroutine Ended Successfully!");
     }
 }
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

Answer by Bunny83 · Sep 07, 2019 at 12:17 AM

There are several issues here. First of all this line makes no sense at all and does nothing except producing garbage:

 StopCoroutine(CountdownTime());

Each time you call "CountdownTime()" you create a new statemachine object. Passing it to StopCoroutine does nothing since this specific statemachine hasn't yet been registrated as coroutine.


About your issue when you see which debug log statement: You first print the value of your variable and then you reduce it by 1. Just switch the order of those two lines. So instead of:

 Debug.Log(CountdownSeconds);
 CountdownSeconds--;

just do:

 CountdownSeconds--;
 Debug.Log(CountdownSeconds);


Another thing you might want to change is to switch your variable type from float to int because you seem to only change your variable in whole units. Using a float could possible create additional issues like getting values like 0.99999999 instead of 1.

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 DissonanceMask · Sep 07, 2019 at 06:18 AM 0
Share

Thanks for your assistance and feedback! I tried switching them as you instructed, but it ended up counting from 2 down to -1. I fixed the -1 by making my while as CountdownSeconds > 0, but I dont know why it doesnt count from my assigned CountDownSeconds.

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

119 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 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

Create a countdown in C# (inside Coroutine) 2 Answers

Making a fill take exactly n seconds to complete 2 Answers

Countdown Timer 1 Answer

countdown/countup timer 1 Answer

How to stop a Countdown Timer? 1 Answer


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