pause time unity code example
Example 1: how to pause your game unity
using UnityEngine;
void Pause()
{
Time.timeScale = 0;
}
void Unpause()
{
Time.timeScale = 1;
}
Example 2: wait time in unity
using System.Collections;
private void Start()
{
StartCoroutine(Wait());
}
IEnumerator Wait()
{
//To wait, type this:
//Stuff before waiting
yield return new WaitForSeconds(/*number of seconds*/);
//Stuff after waiting.
}
Example 3: time stop unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Example : MonoBehaviour
{
bool timeOff; //Set a bool to know when the time is off
public void TimeOff() //Make a public void to reference
{
Time.timeScale = 0f; //Set the time to 0f
}
public void TimeOn()
{
Time.timeScale = 1f; //Set the time to 1f
}
void Start() //You don't need a start method so you can delete it if you want
{
}
void Update() //You do need the Update Method
{
if(timeOff) //We need an if() statement here
{
TimeOff(); //Reference TimeOff();
timeOff = true; //Set the bool to true
}else if(timeOff) //Add an else if statement here
{
TimeOn(); //Reference TimeOn();
timeOff = false; //Set the bool to false
}
}
//Now after the code is saved, click on the button that will trigger your TimeOff() and add a OnClick Event to it and Reference TimeOff
//Now click on the button that will go back to the game and add a OnClick Event to it and Reference the TimeOn() void
}