Timer variable set to zero

I am trying to make a script that does this. Whenever the game starts and enters the scene containing the script then the variable myTimer will be set to zero and so will the Time.time so that the game can be played again from the start with the time counting up from zero instead of from where the last game ended. This is the script I have but it doesn’t do what I want.

using UnityEngine;
using System.Collections;

//ATTACH TO MAIN CAMERA, shows your health and coins
public class GUIManager : MonoBehaviour 
	public GUISkin guiSkin;			//assign the skin for GUI display
	public int coinsCollected;
	public float LastTime;
	public float BestTime; 
	public float myTimer;

	private int coinsInLevel;
	private Health health;
	//setup, get how many coins are in this level
	void Start()
		coinsInLevel = GameObject.FindGameObjectsWithTag("Coin").Length;	
		health = GameObject.FindGameObjectWithTag("Player").GetComponent<Health>();

	void Update()
			myTimer = Time.time;

	//show current health and how many coins you've collected
	void OnGUI()
		GUI.skin = guiSkin;
			GUILayout.Label("Time: " + myTimer.ToString("F2"));
		if(coinsInLevel > 0)
			GUILayout.Label ("Gifts: " + coinsCollected + " / " + coinsInLevel);
		if(coinsCollected > 9)
		    myTimer = 0; 

Try setting myTimer/Time.time equal to 0 in the Start function. That way each time this script is called (i.e. whenever you start the level), it will reset the time.

Side note, in your OnGUI function you have several IfThen statements. The Then statements need to be in {}'s. Technically, it will still compile correctly, but sometimes it will count later If’s as they were nested within the first.

Hope this helps!

Time.time is readonly.

If you want to track the time since the level started the set a variable to hold the value Time.time returns when gameplay starts.

You can then calculate the difference between this variable and Time.time to determine how much time has passed since.