Awake function

So just a quick question of what we have done here. The awake function gets called when the script is loaded and through this code:

equipment = GetComponent<Equipment>();
            if(equipment)
            {
                
                equipment.equipmentUpdated += UpdateWeapon;
            } 

we get notifications and subscribe to the equipmentUpdate but what does that even mean? I dont get it, how UpdateWeapon gets called everytime when we equip/unequip something, when its only been called once in the Awake function in the beginning? or what does Sam actually mean with subscription?

equipment.equipmentUpdated is an event that is called whenever something changes in our Equipment component. When an item is added or removed from Equipment, it calls

if(equipmentUpdated!=null)
{
     equipmentUpdated.Invoke();
}

The line in Awake adds the UpdateWeapon to the list of methods that should be called in that event, so when the equipment is modified, UpdateWeapon is called in this script automagically.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms