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;
    }
    */
}

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);
    }
}

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;
             }
         }
     }

Thank both of you @Hellium and @Kaboka22 :slight_smile: I tried both scripts and it worked perfectly :slight_smile: 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?