Timer Ontrigger

Hi everyone,

I have a timer script in which i have a public void which is called by an Ontrigger event on another script.
I need this public void to start a timer which should be set to 0 before that.

Problem is, my timer starts as soon as I launch the game.

Do you have any ideas on how I should do it ?

(i’m using C# btw)

Thanks a lot

My code is :

public class Timer : MonoBehaviour {

 public Text timerText;
 private float startTime ;
 private bool finished = false;
private bool start = false;

 void Update () {
     
     if(finished)
         return;
	 
     float t = Time.time - startTime;
     
     string minutes = ((int) t/60).ToString();
     string seconds = (t%60).ToString("f2");
     
     timerText.text = minutes + ":" + seconds;
     
 }
 
  public void Start ()
 
 {
	start = true;
	 startTime = Time.time;
 }
 
 public void Finish()
 {
     finished = true;
     timerText.color = Color.yellow;
     
 }   

}

I know I should do something with my void update and my public void start but I don’t really know what it is.

You almost got it ! You can just use the same solution as you stopped your timer. Use your variable bool started as a trigger to start it.

Edit: Also rename your method Start() to something else. Unity automatically call the Start() method when the object is instantiated.

Here is the corrected code:

public class Timer : MonoBehaviour {

	public Text timerText;
	private float startTime;
	private bool finished = false;
	private bool started = false;
 
	void Update () 
	{	  
		if(!started || finished)
			return;
	  
		float t = Time.time - startTime;

		string minutes = ((int) t/60).ToString();
		string seconds = (t%60).ToString("f2");

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

	public void StartTimer ()
	{
		started = true;
		startTime = Time.time;
	}

	public void StopTimer()
	{
		finished = true;
		timerText.color = Color.yellow;	  
	}   
}