Awake() and Start()

后端 未结 4 936
北荒
北荒 2020-12-29 01:02

I see that we can initialize Variable in Awake() or Start() and Awake() will be called before Start().

When shou

4条回答
  •  隐瞒了意图╮
    2020-12-29 01:30

    Usually Awake() is used to initialize if certain values or script are dependent on each other and would cause errors if one of them is initialized too late (awake runs before the game starts). Awake is also called only once for every script instance.

    Let me quote the Documentation:

    [...] Awake is called after all objects are initialized so you can safely speak to other objects or query them using eg. GameObject.FindWithTag. Each GameObject's Awake is called in a random order between objects. Because of this, you should use Awake to set up references between scripts, and use Start() to pass any information back and forth. Awake is always called before any Start functions. This allows you to order initialization of scripts. Awake can not act as a coroutine.

    and about Start():

    Start is called on the frame when a script is enabled just before any of the Update methods is called the first time.

    Like the Awake function, Start is called exactly once in the lifetime of the script. However, Awake is called when the script object is initialised, regardless of whether or not the script is enabled. Start may not be called on the same frame as Awake if the script is not enabled at initialisation time.

    Where the last part makes one big difference

    To get to your question:

    If the script is NOT enabled at the beginning of your game, and you don't need the variables to be initialized, start would be saving performance as awake() would be called regardless...
    every variable would be initialized at the very beginning. At least that's the logical assumption I make.

提交回复
热议问题