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.