Add time to timer

Hello everyone,

I have a question regarding my timer script. The timer should count down from 20 seconds to 0. When second zero is reached, the restart function should be called.

So far so good. Everything works.

Now I have distributed objects on the map, which should add 5 seconds to the timer when they are collected.

I have assigned the entire script to the collection objects.

I have now tried several times and can’t get it to work.

Attached is the script, maybe someone can help.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Timer : MonoBehaviour
{
    public bool runningTimer;
    public float currentTime;
    public float maxTime = 25;

    public HealthSystem healthsystem;

    public Text timerText;


    // Start is called before the first frame update
    void Start()
    {
        currentTime = maxTime;
        runningTimer = true;
    }

    // Update is called once per frame
    void Update()
    {
        if (runningTimer)
        {
            currentTime -= Time.deltaTime;
            timerText.text = "Time: " + System.Math.Round(currentTime,0);
            Debug.Log(currentTime);

                if (currentTime <= 0)
                {
                    runningTimer = false;
                    healthsystem.RespawnPlayer();
                    Restart();
                }
        }
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.tag == "Player")
        {
            currentTime += 5f;
            Debug.Log("Plus 5 Sekunden");
            Destroy(gameObject);
        }
    }

    public void Restart()
    {
        currentTime = maxTime;
        runningTimer = true;
    }
}

.
The error should be in line 46. The debug.log and destroy functions are executed.

Maybe someone can help. Greetings

Hi! You might have posted this in the wrong place; it looks like you tagged this as SpeedTree by accident. I’d edit your your category to Unity Engine.

1 Like

Many thanks for the tip. I can no longer adjust the category. Should I close the thread and open a new one?

Yes, I would recommend that. I’ll let the Discussions developers know that there might be a bug.