Search Unity

does an non existent Update method affect the performance?

Discussion in 'Scripting' started by craig4android, May 16, 2019.

  1. craig4android

    craig4android

    Joined:
    May 8, 2019
    Posts:
    124
    does an non existent Update method affect the performance? Like when I add a script derived from
    MonoBehaviour, but don't add the Update method, will it affect the Performance of the render loop?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Are you asking if a script doesn't have Update, would if effect performance?

    Scripts are never required to have Update, Awake, Start, or any of the other "magic methods" that Unity uses. It's actually better to remove those methods if they are empty and not doing anything as they do still get called.
     
  3. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    If they are empty they will be added do the update queue and will add to the render loop complexity. If you don't have a update method it will not be added to the queue and not contribute any complexity.

    Though it will take some memory etc. Most often neglectable
     
    KiyanOne likes this.
  4. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,998
    It obviously depends on how the system works. You could imagine that each frame it scans all monobehaviours, checking whether they have an Update to run. If that was the case, any monobehaviour would waste a little time during the Update cycle.

    But the system finds your functions through reflection, since they aren't public. And that's slow. So what it probably does is search for Update once, when you create the object. There's probably a secret list of "these have an Update() to run". If your monobehaviour doesn't have Update(), it wouldn't be on the list and would cost 0 extra time each frame.

    I suppose you could spawn 100,000 invisible items with Update(){} and again without, and compare.
     
    Ryiah likes this.
  5. craig4android

    craig4android

    Joined:
    May 8, 2019
    Posts:
    124
    ty, was just wondering
     
  6. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    It's already been mentioned, but having an empty Update method in a script will definitely waste some performance. I once had thousands of the same object in my scene, and found that a big portion of the expense of those objects was that they had Update methods that didn't do anything. Removing the Update improved performance significantly.