0

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.

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

Hmmm... doesn´t work for me? Maybe because I built on Android?
I have the script with the OnEnable() on the GameObject that will be set active, thats right isnt it?
Found my mistake I made it IEnumerator. Made it void and started a coroutine there and it works!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.