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.
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.
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
}
}