Two easy questions :P

1.- How to make Update() function update every X seconds?

2.-How to make the function Update() stop updating if something happens?

Thank you in advance :smile:

  1. look up “coroutines” in the learn section

//psuedo code
if(some circumstance is true)
{
do stuff
}

if(something happens) 
{
circumstance is false
}

alternatives:
1)

InvokeRepeating("SlowUpdate",x,x);
void Update()
{
    if (isDead) return; // early exit update loop when bool isDead is true

   // more code..
}

or can disable script, so Update() is not running anymore:

this.enabled = false; // disable script
2 Likes

Thanks a lot!

If you want to do this for all your objects, you could use FixedUpdate with a time step of X seconds.

An alternative way could be something like this:

private float timer;
private float updateRate=5f; // update every 5 seconds

void Update()
{
     timer+= Time.deltaTime;
     if(timer>updateRate)
     {
         timer=0f;
        // Do something
     }
}
1 Like