Unger Games
Unger Games

Reputation: 11

Unity start function after set a Gameobject active

I want a function like Start() (So it only calls it once) after I set my GameObject active. How can I do this?

Thought doing it within the Update with an if statement so if GameObject is active it will be called. But the problem is it will call it not once.

Upvotes: 0

Views: 4292

Answers (1)

Umair M
Umair M

Reputation: 10750

Use OnEnable() method as it is only called once when the object is enabled:

void OnEnable()
{
    //This will be called when object is enabled.
    Debug.Log("OnEnabled()", gameObject);
}

Update:

OnEnable() is just another Unity method like Start() but its called every time the object activates. watch this: https://www.youtube.com/watch?v=GPiHgMIqj2E

Upvotes: 4

Related Questions