1
\$\begingroup\$

Here is the script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class Timer : MonoBehaviour
{
    public static Timer instance;

    public TextMeshProUGUI TimerText;
    private float startTime;

    private void Start()
    {
        instance = this;

        startTime = Time.time;
    }

    private void Update()
    {
        float time = Time.time - startTime;

        string minutes = ((int)time / 60).ToString();
        string seconds = (time % 60).ToString("f0");

        TimerText.text = minutes + ":" + seconds;
    }
}

I tried quite a few things and none of them worked. I'm kind of new to C# and unity and I would like to know how to stop this timer from another script. And I want to check if the game object the script is on is enabled.

Any help is appreciated :)

\$\endgroup\$
1
  • 1
    \$\begingroup\$ "I tried quite a few things" does not help us avoid telling you to do things you've already tried, because we don't know what those are. "...none of them worked" doesn't tell us what went wrong, which is information we need to be able to help you solve those problems. Be sure to actually explain what you tried and how specifically it failed. \$\endgroup\$
    – DMGregory
    Nov 1, 2021 at 12:56

1 Answer 1

2
\$\begingroup\$

First, store a reference to the game object that has the timer you want to stop:

public Timer timerToStop;

You can fill this in by dragging the object with the timer into this field in the inspector. You can also have another script set this variable, or find the timer with FindObjectOfType<Timer>().

Next, you simply disable it:

timerToStop.enabled = false;

This will prevent its Update() method from ticking, so the displayed time value will freeze at the last time it had. This will not out of the box support resuming the timer, but that wasn't included in your question.

Since you seem to be using a singleton pattern here, you could also write

Timer.instance.enabled = false;

...without storing/looking up your own reference to it. But this limits you to having just one timer, which may or may not be what you want.

You can check whether the timer script is enabled just by reading the value of its enabled property.

You can check whether the game object it's on is active by checking timerToStop.gameObject.activeInHierarchy - this will return false if the game object or any of its parents in the hierarchy have been set to inactive. If you care only about this game object itself and not those above it, you can use .activeSelf.

\$\endgroup\$
3
  • \$\begingroup\$ This is the right answer :) I voted to delete my shorter simultaneous response... \$\endgroup\$ Nov 1, 2021 at 13:25
  • \$\begingroup\$ Sorry to stomp you there, Acme Nerd Games! I kind of wish SE had a notification "Another user is drafting an answer..." so I don't accidentally duplicate stuff other users are working on. \$\endgroup\$
    – DMGregory
    Nov 1, 2021 at 13:45
  • \$\begingroup\$ No worries DMGregory -- I was trying to compose an answer on my phone, which is a kind of an exercise in futility anyway. \$\endgroup\$ Nov 1, 2021 at 13:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .