14
Jul

C# Awake and Start in Unity! – Beginner Scripting Tutorial



– [Narrator] Awake and
start are two functions that are called automatically
when a script is loaded. Awake is called first even
if the script component is not enabled and is
best used for setting up any references between
scripts and initialization. Start is called after awake, immediately before the first update. But only if the script
component is enabled. This means that you can use
start for anything you need to occur when the script
component is enabled. This allows you to delay any
part your initialization code until it's really needed. For example an enemy
character could enter the game and use awake to have ammo
count assigned to him, but only get the ability
to shoot using start at a defined time when that
script component is enabled. It should be noted however that start and awake will
only ever be called once in the lifetime of a script
attached to an object. So you cannot repeat the start function by disabling and re-enabling a script. In this example we have a script that simply has awake and start functions. We're using Debug.Log to show
when those two things occur. For example without this
script component enabled, we will simply see the awake function. But if I enable the script, I will see that both
functions have been called. This is useful as it allows you to initialize settings for an object before enabling that script component, without the need for splitting the script into several different scripts.

Tags: , , , , , , , , , , , , , , , , , , , ,

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *