- Home /
The question is answered, right answer was accepted
C# OnBecameVisible turning of particles and effects
What im trying to do is while the object is not in view to not play animations,effects or particles.I have read a similar question with this response: " It is a reasonable option, but keep in mind the GameObject will not update during the time it is off camera view. Therefore, if say that GameObject needs to do something during update, it will not happen. Instead, you could give the GameObject an "Off" state which is activated when no longer in camera view. Allowing it to continue to run Updates if needed, but is not actually disabled. " I do not mind the objects not update since those objects dont have specific scripts i need to be using(most of them are just lights). Please help me by telling me how i can disable them while OnBecameInvisible() function is called.Thank you.
Answer by GameVortex · Jan 14, 2014 at 08:16 AM
I am a little unsure of what you are trying to accomplish with turning everything off like that. Animations by default does not play while off screen and particles are not rendered.
But it seems you have mostly figured it out yourself though: Disable the object when OnBecameInvisible() is called. For that you use the gameObject.SetActive(false) function.
Example:
private void OnBecameInvisible()
{
gameObject.SetActive(false);
}
This disables everything on the gameObject though. Which means the OnBecameVisible will never be called as the objects renderer is turned off.
Maybe you only want to disable animations and particle effects explicitly?
Do that by finding the animation or particle system component and disabling them.
Example:
private void OnBecameInvisible()
{
ParticleSystem ps = GetComponent<ParticleSystem>();
if(ps)
{
ps.Stop();
}
//Monobehaviour already has a variable to animation
if(animation)
{
animation.Stop();
}
}
Then you can turn them on again when the object becomes visible.
yes i only needed to turn the animation and the particles as I have a ton of torches in my scene(light+animations) thank you.
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Get ParticleSystem (Shuriken) to play from array of game objects C# 2 Answers
Very strange particle question. 0 Answers