- Home /
How do I enable or disable a ParticleRenderer attached to an object in script?
I want to disable the ParticleRenderer attached to GameObject(Ball1) in script.
ball1 = GameObject.Find("Ball1");
ball1.ParticleRenderer.enabled = false;
that second line gives an error. How do I do this. Thanks
Answer by · Oct 08, 2010 at 07:43 AM
Using GetComponent works:
ball1 = GameObject.Find("Ball1");
ball1.GetComponent(ParticleRenderer).enabled = false;
If you're using #pragma strict or on the iPhone, you'll need to cast it as a Renderer:
(ball1.GetComponent(ParticleRenderer) as Renderer).enabled = false;
Depending on your setup, you could instead disable the ParticleEmitter:
ball1.particleEmitter.enabled = false;
This gives error 'enabled' is not a member of 'UnityEngine.Component'. The second option gives no error but the particles are still working. I am using the code from the Lightening Bolt Demo in the Procedural Folder. While in game mode, unchecking the Particle Emitter or the Lightening Bolt script doesnt stop the bolt, but unchecking the Particle Renderer does.
Are you using Unity3? The GetComponent approach works fine for me - tested locally. Perhaps you can post your full code in your question?
Actually, are you building for iPhone? If so, take a look at this: http://answers.unity3d.com/questions/3138/why-am-i-getting-x-is-not-a-member-of-y-on-unity-iphone-when-it-works-regula
Unity3 for windows not pro. Those are my only 2 lines of code in my script that reference Ball1 outside maybe the Lightening bolt script. I thank you for testing locally I. Will check for a possible syntax error when I get home tonite.
The iPhone clue helped. The script has '#pragma strict' in it so same rules apply. The line that worked is. (ball1.GetComponent(ParticleRenderer) as Renderer).enabled = false;