I have a question on OnDisable. In the tutorial, Roguelike part 9 @ 2:50 min, the tutorial teacher says that OnDisable is part of the Unity API But then he defines a function called OnDisable. Is he overwritting the function? He clearly is not. I dont get it. Code (CSharp): //This function is called when the behaviour becomes disabled or inactive. private void OnDisable () { //When Player object is disabled, store the current local food total in the GameManager so it can be re-loaded in next level. GameManager.instance.playerFoodPoints = food; } Thanks in advance
Hi @squarelife In practice this means Unity engine will call this method in it's update loop (if object gets disabled), if it exists in your MonoBehaviour script attached to your gameObject. Maybe someone can give you more technical answer, but this is what happens. https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnDisable.html
so if gameobject.enabled = false. The Unity Engine loop will call the function OnDisable if I implemented it? So in other word its a function you implement that you dont need to call because the Unity loop will call it for you?
It's one of the Monobehaviour's built-in event-handling methods. It's just like Update or OnTriggerEnter except it specifically runs when an object is disabled. These type of methods are special to monobehaviours. There is a manual page that explains them here: https://docs.unity3d.com/Manual/EventFunctions.html
Also there is a list of "magic methods" in the manual if you will need them: https://docs.unity3d.com/ScriptReference/MonoBehaviour.html