- Home /
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;
}
*/
}
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);
}
}
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?
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.
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;
}
}
}
Your answer
Follow this Question
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