Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

OnDisable

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

  1. squarelife

    squarelife

    Joined:
    Nov 8, 2017
    Posts:
    13
    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:
    1,001
    squarelife likes this.
  3. squarelife

    squarelife

    Joined:
    Nov 8, 2017
    Posts:
    13
    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:
    461
    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:
    461
    Yes,
     
    squarelife likes this.
  6. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    2,682
    SparrowsNest likes this.
  7. SparrowsNest

    SparrowsNest

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