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 NumerobisekCZE · Apr 29, 2020 at 08:34 PM · timerslidercountdown

Timer (slider) wont start

Hello everybody, i am still beginner in Unity so i apologize to advanced programmers with maybe stupid basic question. I am trying to create application in Unity and part of the app is displayed timer (in form of slider) which should move from full (maxValue = 60) to zero. Timer starts after push of the button and it should go down for 60 seconds. I created few Debug logs to track progress of code but I only get to number "3". PS. this is my first post here so i really apologize if i write some formatting here wrong. Thanks for all the help and your suggestions. As you may see english isnt my first language.

here is my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class TimeSlider : MonoBehaviour
 {
     public Slider slider;
     int maxTime = 60;
     bool moveSlider = false;
 
     public void Start()
     {
         Debug.Log("1");
        StartCoroutine("countDown");
         slider.maxValue = maxTime;
     }
 
     private IEnumerator countDown()
     {
 
         Debug.Log("2");
         while (moveSlider==true)
         {
             Debug.Log("4");
             slider.value = maxTime - 1;
             yield return new WaitForSeconds(1f);
             if (slider.value < 0)
             {
                 Debug.Log("5");
                 moveSlider = false;
             }
         }
         
 
     }    
 
     public void startTimer ()
     {
         moveSlider = true;
         Debug.Log("3");
     
     }
 
     /*
     public void resetTimer()
     {
         moveSlider = false;
         slider.maxValue = maxTime;
     }
     */
 }

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

2 Replies

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

Answer by Hellium · Apr 29, 2020 at 09:07 PM

 using System.Collections;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class TimeSlider : MonoBehaviour
 {
     public Slider slider;
     const int maxTime = 60;
     IEnumerator countDown;
 
     private void Start()
     {
         slider.maxValue = maxTime;
     }
 
     private IEnumerator CountDown()
     {
         WaitForSeconds wait = new WaitForSeconds(1f);
         for( ; slider.value > 0 ; slider.value -= 1)
             yield return wait;
         countDown = null;
     }
 
     public void StartTimer ()
     {
         StopTimer();
         countDown = CountDown();
         StartCoroutine(countDown);
     }
 
     public void ResetTimer()
     {
         StopTimer();
         slider.maxValue = maxTime;
     }
 
     private void StopTimer()
     {
         if(countDown != null)
             StopCoroutine(countDown);
     }
 }
Comment
Add comment · Show 2 · 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 NumerobisekCZE · May 01, 2020 at 06:35 PM 1
Share

Thank both of you @Hellium and @Kaboka22 :) I tried both scripts and it worked perfectly :) Can i just ask you @Hellium why you set Cooritune to null? Do I understand correctly that setting courutine to null is same as stopping it and having opportunity to check if its null (stopped)? So its basicly the same thing as StopCourutine but you can now check if it runs or not?

avatar image Hellium NumerobisekCZE · May 01, 2020 at 06:42 PM 0
Share

Setting the coroutine to null does not stop it.


I set it to null so that ResetTimer does not call StopCoroutine if the coroutine is not currently running.


I should do the same on StartTimer BTW, otherwise, you could have two coroutines running at the same time. I will edit my answer.

avatar image
0

Answer by Kaboka22 · Apr 29, 2020 at 09:08 PM

I think the problem here is that you're giving the slider the same value every time, since you don't change the value of maxTime. Maybe try something like this:

      private IEnumerator countDown()
      {
          int currentTime = maxTime;
          while (moveSlider)
          {
              currentTime--;
              slider.value = currentTime;
              yield return new WaitForSeconds(1f);
              if (slider.value < 0)
              {
                  moveSlider = false;
              }
          }
      }

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

130 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Start timer with trigger 1 Answer

How to stop the countdown timer when I have collected all the coins? 2 Answers

I want to pick up objects to increase timer. 2 Answers

how to Subtract string from TimeSpan 0 Answers

Calling methods 0 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