How to Stop Timer when User Collides with Objects ?

Hello everyone

I have been searching and tried almost everything that i could find but none of them has been worked.

I cannot let my timer stop after my player collides with objects, walls or something else. All of items have rigidbody2d and box collider 2d. Is trigger off because otherwise the player passes away from those objects.

I would be grateful if you could help.

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

public class Timer : MonoBehaviour
{
    public Text counterText;
    public bool TimerOn;
    

    public float seconds, minutes;

    void Start()
    {
        TimerOn = false;
        Text counterText = GetComponent<Text>();
        counterText = GetComponent<Text>() as Text;
    }

    void Update()
    {
        if (TimerOn = true)
        {

            seconds = (int)(Time.time % 60f);
            counterText.text = seconds.ToString("0");
        }
    }

    void OnCollisionEnter2D(Collision2D col)
    {

        if (col.gameObject.tag == "Player")
        {
            TimerOn = false;
        }
    }

}

You have TimerOn = true;
It should either be == or just

if(TimerOn)

You are simply setting TimerOn to true constantly.

i have handled it.

and i have used “Time.timeSinceLevelLoad” method instead of Time.time.

I have learnt that, with this method the timer gets resetted every time user clicks on restart button. Exactly as i want.

Thank you very much for your lovely answers. This is an amazing community.
@SamohtVII
@tormentoarmagedoom
@Harinezumi

i am quite confused about the script.

Timer script is working fine except the part of collide.

Shall I drag this script to every object that i should let the timer stop when collide ?

Or do we need another script to freeze time when player collide ?

Thank you.