- Home /
When OnBecameInvisible been called
Hi All,
I have this weird thing goining on. I use OnBecameInvisible when my character is no longer seen by the main camera. When it is not visible bu the main camera I start a coroutine:
void OnBecameInvisible()
{
someComponent.enabled = false;
StartCoroutine(SomeCoroutine());
}
It works great utill I stop running the game in the editor (when I press the play button again to stop running the game) and I get this console error :
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) guy is the name of my character where the script is attcahed. Any ideas? Thank u in advanceCoroutine couldn't be started because the the game object 'guy' is inactive!
Answer by magavelis · Apr 10, 2013 at 03:40 AM
Probably an answer is too late, but today i spent some time with the same issue and find out a solution by using this method:
void OnApplicationQuit() {
activated = false;
}
need to add if statement in your OnBecameInvisible() method, like this:
void OnBecameInvisible() {
if(activated == true) {
// your code
}
}
don't forget to initialize "bool activated = true;" variable at beginning of the script.
Although I don't like the fact that OnBecomeInvisible get's called by default when application quits, it does kind of make sense. In any case I've used your workaround to get rid of the pesky error message at each editor quit. Thanks.
I was going nuts trying to figure out what it was ...
I tried OnDisable()
and OnDestroy()
... but didn't know about OnApplicationQuit()
Thanks for this
Answer by Maulik2208 · Dec 31, 2012 at 08:46 AM
you have Disabled the Guy by script but you didn't enabled it after Coroutine so ... SomeComponent.enabled = false; StartCoroutine(SomeCoroutine()); someComponent.enabled = true;
And Enjoy....Cheers.....Don't forget to mark the answer if helped you.....
Thank u for your answer but I don't disable Guy (name of game object) only one component of it. $$anonymous$$oreover I enable the component at the end of the coroutine..
The console error above when i stop running the game in the editor
Guy is active at the beginning of the game. its active all the time that why I don't understand the console error at the end of the game (when I press play the second time to stop the game in the editor)
Your answer
Follow this Question
Related Questions
A loop FOR waits for a coroutine to end 1 Answer
When using coroutines, what should I be wary of? 2 Answers
coroutine->IsInList() 0 Answers
Time.deltaTime not returning time since last frame? 1 Answer
How to hit game object in coroutine 2 Answers