Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to make a function triggered when you activate/deactivate a GameObject

Discussion in 'Documentation' started by Wailor, May 2, 2015.

  1. Wailor

    Wailor

    Joined:
    May 2, 2015
    Posts:
    5
    Hi everyone.

    I am making a game and want to use only one scene, as the continuous creation and destruction of objects is quite expensive and may even cause memory errors (correct me if I am wrong, but that's what they told me in college)

    So, to emulate the desired behaviour, I made this:
    Scene -> Empty GameObject
    GameObjects in a Scene -> Children of the scene GO
    Scene Change -> Activate and deactivate Scene GO (and by extension all their children)

    The thing is, I want to know if it is possible to make a function that is triggered every time you activate a GameObject (what in a normal architecture would be done by the Start function)

    Thanks in advanced and sorry for bad English :p
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,559
    It is possible, check out OnEnable and OnDisable.

    Check out this link for reference on the order of MonoBehaviour callbacks: http://docs.unity3d.com/Manual/ExecutionOrder.html
     
    Wailor likes this.
  3. Wailor

    Wailor

    Joined:
    May 2, 2015
    Posts:
    5
    Thanks man. That's exactly what wanted :)
     
    liortal likes this.