Update with bool flag or script enabled/disabled?

Hi all, I wonder how to optimize my code and I’m trying to figure how to do it.
Usually I use this in the update, something like:

void Update(){
    if(bool flag){
        //do something
    }
}

but in this way in every update frame the if condition will be checked and usually what it does it’s executed only one time or very few times.

what about use a script like:

void Start(){
    this.enabled = false;
}


void Update(){
    //do something
    if(finish){
      this.enabled = false;
    }
}

and enable this script by another one??

Hi,

Yes I think your approach is a good one. It’s better to disable the scripts rather than rely on a flag. Admittedly the time taken to invoke the Update method and then execute the IF statement is unlikely to be significant, but it is mentioned in the Wiki General Performance Tips:

Beware the empty Update functions. When creating new scripts with the Assets menu, they include an empty Update() function. Remove it if you don’t need it as it comes at a (rather light) performance cost. This performance cost applies to all overridable functions in MonoBehaviour scripts, with Update and FixedUpdate being the main targets.

I’ve provided some (hopefully!) useful advice for a number of scenarios…

  1. Does the code run literally once and then stop?
  2. If the code runs multiple times, is it triggered by an event or a timer?
  3. If it’s triggered by a timer, are there lots of scripts in this position?

(1) If the code in the Update method is going to run literally once and never again, then a self-disabling script achieves that goal with no added complexity.

(2) If the code is triggered by an event then ditch the Update method and define your own method e.g. DoSomething(). Then in the script where the event is detected use GameObject.Find and GetComponent in order to call the script directly. No need to disable the script now. (Cache the results to any Find/Get methods.)

(3) Finally if it’s a timer you need then there’s a couple of ways (as always!). First you’ll need an empty game object, call it Timer, and attach a script. Then,

(3a) The simplest mechanism would be to implement an Update method which checks the time elapsed on each frame and uses that to choose whether to invoke any of the other scripts. Just like with (2), the scripts being called should not declare an Update function, hence there’s no need to disable them. This doesn’t change the Update/flag approach, it just consolidates it down so you’ve only got once script getting hammered! Call the other scripts using GameObject.Find and GetComponent. Cache the results to any Find/Get methods.

(3b) A better approach might be to implement a Start() method which launches a Coroutine. This coroutine is still responsible for working out when the next method needs to run, but it returns this to Unity via a yield new WaitForSeconds(x). This has the affect of having Unity work out when next to call your code, which negates all the calls to Update.

Thank you very much!!! a lot of useful information, usually i’m in the second case, an event, like a mouse click, to start an action on a GameObject. Maybe there is also a 2b) by the fact that usually working on a gameobject you need Update() or FixedUpdate() to move,rotate,lerp(etc etc) something, but then you need to disable the script to stop the update or it will continue to work!