Search Unity

Does Update() run on GameObjects without a script component?

Discussion in 'Scripting' started by IngeJones, Apr 27, 2015.

  1. IngeJones

    IngeJones

    Joined:
    Dec 11, 2013
    Posts:
    129
    I am trying to decide whether to offload some scripted processing from a control object to a scene gameobject - lets call it "MyObject" that does not yet have a script component. As there will be several of MyObject, I wanted to know whether it would suddenly cause them all to be responding to an Update event (albeit with an empty function) they wouldn't otherwise be bothering with, and therefore putting more load on the game. Or does any Unity GameObject listen for Update anyway, whether or not there is an actual custom script attached?
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    If a GameObject has a script on it with an Update function, that Update will run.

    afaik, if you have a MonoBehaviour without an Update function, then there won't be any attempts to run Update. Remember, Update is not a virtual function inherited from MonoBehaviour, but a function with a special name that Unity Magic makes run every frame.

    How are you doing this offloading, though? You usually want to put stuff that needs to run on another thread, not another object. Your scene object and control object runs on the same main thread, so if you're doing the same thing, moving the job to the gameobject probably won't improve performance.
     
    IngeJones likes this.
  3. IngeJones

    IngeJones

    Joined:
    Dec 11, 2013
    Posts:
    129
    When I speak of offloading I was actually referring to bulk of code (for ease of maintaining it) rather than processing, sorry I expressed it badly. Thanks for your reply that is exactly the information I needed :)
     
  4. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    If you do not define an Update() function, it will not attempt to run it, no. Same with any other of the called-by-unity methods.

    Unity uses reflection to poll your MonoBehaviours after they've been compiled for implemented events and remembers which ones have them and which ones don't.
     
    avvie and IngeJones like this.
  5. IngeJones

    IngeJones

    Joined:
    Dec 11, 2013
    Posts:
    129
    Thank you :)