Search Unity

Custom functions vs just Update

Discussion in 'Scripting' started by Denisowator, Feb 10, 2019.

  1. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    Is there any benefit to creating a custom function (that's not a coroutine) and calling it through Update, rather than just having the code run in Update? Other than making the code look nicer and more organized.
     
  2. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    Honestly theres not much more to it than that. Making it easier to move said code too if you want to change where it is called. If the code is used in multiple places etc.
     
    Denisowator likes this.
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    If you're talking about making something like this:
    Code (CSharp):
    1. void Update(){
    2.  
    3. DoStuff();
    4. }
    5.  
    6. void DoStuff(){
    7. //Do your stuff here
    8. }
    It's basically the same thing as if you were to "DoStuff" in the Update() loop, only with an extra function call

    If you're talking about one monobehaviour getting the Update method from Unity and calling a custom update loop on all other object you get some performance boost proportional to how many Updates are changed into a custom update.

    Pro tip: a search engine is your friend (be it google, the forums or whatever)
    https://forum.unity.com/threads/anything-wrong-with-dispatching-update-s-manually.620686
     
    Denisowator likes this.
  4. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    It depends, let's say you have a system, like a ballistics systems. Then when a gun is fired its better to put that request on a queue and let some kind of worker take care of the work than each gun should handle it's own ballistics in a Coroutine or update method. It's basicly what's ECS is doing.
     
    Denisowator likes this.