Search Unity

Overhead of Scripts with no Update()

Discussion in 'Scripting' started by Brian-Brookwell, Nov 2, 2017.

  1. Brian-Brookwell

    Brian-Brookwell

    Joined:
    Sep 14, 2012
    Posts:
    47
    I'm currently working on a Sensor System and am wondering if there's any significant overhead to creating an empty gameObject with each sensor? Each sensor script would have no Update so I assume the Game Loop would be calling an empty Update in Monobehaviour.
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    A very small one.

    If the gameobject has no renderers, scripts, colliders, or any of that attached, their cost impact should pretty much be:
    - All Find methods take longer, since they need to look through more objects
    - The scene takes longer to load, since it's larger
    - The scene takes more space in memory, since it's larger.
    - It's not free to move them, even if they're moved because they're the child of some other object.

    Now, all of these are really, really insignificant per GO, and is only relevant at scale. If you have like... a couple of dozen of things that have these sensors, you shouldn't worry at all. If you have a thousand, you might want to rethink it.


    The big question is if these empty GameObjects make your code easier to read. Are you using them just to mark positions? Could you not just use a Vector3? Or would that make your code significantly uglier?
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Nope, no update would be called. Unity doesn't even bother to attempt to call update if no update exists for the script.

    As @Baste said, the overhead is minimal, and really only impacts memory and the actual loading of the scene. And that memory footprint is very tiny per GameObject (talking a few bytes at most).

    Our scenes in our games are full of GameObjects used for things like sensors, visual scripting tools, or just general old place markers. I really haven't seen any major overhead from them in general.
     
    sean244 likes this.
  4. Brian-Brookwell

    Brian-Brookwell

    Joined:
    Sep 14, 2012
    Posts:
    47
    I'm using the game object to simplify the sensor math so that the forward of the gameObject determines the facing of the sensor. Good to know that the cost is really minimal. I'll write it with MonoBehaviour then and do some test timings with a "herd" of sensored objects
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Just remember, empty Updates are still called, so just remove Update if you aren't using it. Same with Start and Awake.
     
    Rukas90, TheMemorius and sean244 like this.