Gameplay Timer?

Hi, I’m working on a game and I want to be able to increase the difficulty (speed) of the game depending on how long someone has been playing for… I can’t use the Real time since startup, because I want it from the moment they push “Start Game” rather than have the timer start as the whole game loads up… And I’d also like the timer to pause between levels…

Is there an easy way to do this? (I know only basic Javascript right now…) Say every 5 minutes of gametime, the game will run 10% faster, if someone could help me out, I’d greatly appreciate it.

Simply make a float value in Start() (when script is enabled) and say

private var cachedTimeStamp : float;
function Start()
{
    cachedTimeStamp = Time.time;
}

Doesnt matter what the time was when you did this, you can wait a relative amount of time.

if(Time.time >= cachedTimeStamp + 300) //5 minutes passed since cache.
{
    //whatever
}

Of course Time.deltaTime is the time difference between a frame. Can be very very useful.

var timer : float = 0;

    function Update()
    {
         timer += Time.deltaTime;
    
         if(timer >= 300)
         {
               //whatever, every 5 minutes.
               timer = 0; //reset timer
         } 
    }

Ep. 6 Start and Pause the timer by using toggle button - Unity 5.5 for beginner - YouTube - my tutorial link - click UI button to start the timer and use “stopTimer()” to pause your timer. set it to zero to reset the timer.

video-- click button to start the timer and pause the function to pause the timer

here, I had an example to start the timer with UI button. I create a function to pause the timer too.
this function “stopTimer()” you can call to pause the timer. Hope this script can solve your problem.****

This is my code…

  1. Drag the script to the main camera - make sure unchecked the script
  2. add object (main camera) to the button click there, select the function toggletimer.enabled.
  3. if you want to pause your timer, call “stop timer function.”

if you want for details, you can watch my video. All important steps are zoom, below is my script.

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

public class toggleTimer : MonoBehaviour {

	float timer = 0;
	public Text timerText = null;
	int intTimer = 0;
	int Counter = 0;
	int ifstopTime =0;
	int currentTime =0;
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		timer += Time.deltaTime;
		if (ifstopTime == 0) {
			changeText ();
		} else {
			timerText.text = "pause";	
		}
	}

	public void changeText()
	{
		intTimer = (int)timer + currentTime;
		timerText.text = intTimer.ToString ();
	}

	public void stopTimer()
	{
		Counter++;
		if (Counter % 2 == 0) {
			ifstopTime = 1;
		} else {
			ifstopTime = 0;
		}
		currentTime = intTimer;
		timer = 0;
	}
}