- Home /
Pause a script without pausing the whole game
Is it possible to pause a certain script? To let it repeat it's Update function as normal until a certain event happens (for example, a variable hits a certain value) and then freeze the script (but not the whole game) for an amount of time before letting it continue as normal again.
There are various ways you can achieve this kind of thing, some that spring to $$anonymous$$d are:
1) disable and then reenable the component
2) add your own flag that's checked in Update before it does what you want (this would allow you to pause some functionality but not other bits)
3) use a coroutine with an infinite loop, and kill the coroutine (either by explicitly stopping it or setting a flag that makes it break out) then restart it
Answer by nztree · Oct 17, 2016 at 11:59 AM
Well you can't "pause a script" but there are ways to achieve what you're looking for fairly simply.
For example, let's say you want a script on a gameobject to stop when a counter gets to 10.
int value;
void Update(){
if (value >= 10)return;
value++;
//other stuff
}
This will break your update loop once the value reaches 10, so anything else below will be 'paused'.
Alternatively, if you were looking for something to pause for a certain period of time, you could use a coroutine. For example:
bool gameRunning;
void Start(){
gameRunning = true;
StartCoroutine(doSomething());
}
IEnumerator doSomething(){
while(gameRunning){
//do something here
yield return new WaitForSeconds(10);
}
}
This will run your loop in doSomething while gameRunning is true, but pause for 10 seconds each loop.
Hope this helps.
$$anonymous$$aybe this code worked with previous versions but with newer versions it doesn't work. I've been trying to use this code and it gave me an error: error CS0119: Expression denotes a type', where a
variable', value' or
method group' was expected I've been trying to fix this for some time and I was looking for solutions in the Internet until I found out what you need to write "new" in front of "WaitForSeconds". And when I replaced
yield return WaitForSeconds (addPauseTimeHere);
with
yield return new WaitForSeconds (addPauseTimeHere);
it worked perfectly. Still thanks for the idea of coroutines!
Oh you're absolutely right. Just a mistake in the write up on my part. Will amend to fix.
Answer by FirePlantGames · Oct 21, 2016 at 07:56 PM
So. You can do this many ways. If you just want to stop the Update from running, but keep some other functions running you can use a bool variable. Like so; public bool updateScript;
void Update()
{
if (!updateScript) //If the UpdateScript variable isn't true
return; //Then we don't continue with the rest of our code
}
Or. You could enable/disable the script by having one script control the state of the script you want running
public Behaviour behaviourToEnableOrDisable; //The script you don't want to run at certain times
void Update ()
{
if (Input.GetButtonDown("Fire1")) //if left mouse button is clicked
{
behaviourToEnableOrDisable.enabled = !behaviourToEnableOrDisable.enabled //Toggle the script
}
}
Your answer
Follow this Question
Related Questions
A Proper Way To Pause A Game 9 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers