Search Unity

OnDisable

Discussion in 'Scripting' started by squarelife, Jan 11, 2019.

  1. squarelife

    squarelife

    Joined:
    Nov 8, 2017
    Posts:
    41
    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):
    1.  //This function is called when the behaviour becomes disabled or inactive.
    2.         private void OnDisable ()
    3.         {
    4.             //When Player object is disabled, store the current local food total in the GameManager so it can be re-loaded in next level.
    5.             GameManager.instance.playerFoodPoints = food;
    6.         }


    Thanks in advance
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    squarelife likes this.
  3. squarelife

    squarelife

    Joined:
    Nov 8, 2017
    Posts:
    41
    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?
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    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
     
    squarelife likes this.
  5. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    Yes,
     
    squarelife likes this.
  6. SparrowGS likes this.
  7. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    It will be called ONCE when you disable it, it wont keep looping the function