How to alter a timer within a different script

I have a timer script that goes down throughout my game, however I need the time to increase when an event happens.

Timer.cs script:

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
using static UnityEditor.Experimental.GraphView.GraphView;

public class Timer : MonoBehaviour
{
    public GameObject Mouth;
    public GameObject SadMouth;
    public GameObject Pizza;
    
    public Image timerBar;
    public float maxTime = 50f;
    float timeLeft;
    public Animator animComponent;

    // Start is called before the first frame update

    void Start()
    {
        timerBar = GetComponent<Image> ();
        timeLeft = maxTime;

        SadMouth.SetActive(false);

        //       if (PlayerPrefs.HasKey("timeValue"))
        //       {
        //           timeLeft = PlayerPrefs.GetFloat("timeValue");
        //       }
    }

    // Update is called once per frame
    public void Update()
    {
        if (timeLeft > 0)
        {
            timeLeft -= Time.deltaTime;
            timerBar.fillAmount = timeLeft / maxTime;
        }
        else
        {
            Time.timeScale = 0;
        }

        if (timeLeft <= 40)
        {
            animComponent.SetBool("IsLiving", false);
            Mouth.SetActive(false);
        }
        if (timeLeft <= 39)
        {
            SadMouth.SetActive(true);
        }

        //    PlayerPrefs.SetFloat("timeValue", timeLeft);
    }
}

The event that is happening is referenced here, within a different script

if (Input.GetMouseButtonUp(0))
{
    transform.position = originalPos;
    this.dragging = false;
    if (collided)
    {
        Debug.Log("Collding while mouse is up");
    }
}

I need to add time to the timer when collided. I’ve looked on different platforms but can’t seem to figure it out. Any help would be appreciated.

In order for one script to influence another script, you need two things:

  1. ScriptA needs a public interface for the thing you want to change.
  2. ScriptB needs a reference to ScriptA

It is typically a good idea to define a public method rather than change variables in other scripts directly. This gives you more control on the interaction. For example, in your timer script you could add a method like this:

public void AlterTimer(float value)
{
    //value could be positive or negative, so this could be used to increase
    //or decrease the current timer.
    timeLeft += value;
}

In order to access this method, your other script will need a reference to the Timer instance. If both objects are in the same scene, then you can add a [SerializeField] for the Timer in your other script, like this:

[SerializeField] private Timer myTimer;

This should create a field in the inspector where you can drag the object with the Timer script. Then to call the function it is:

myTimer.AlterTimer(5f); //pass in whatever value you need

If your situation has some special conditions, then another solution may be required, but this is often good enough for simple cases.

This worked perfectly, thank you! I’ve been stuck on that for ages ^^