- Home /
An array for Particle Emitters?
I am trying to turn off multiple Particle Emitters in the children of an object at the same time using an array. I will be using the same code to turn them back on later, but I can't get either to work.
Here is the code I am using:
var particleEmitters : ParticleEmitter[];
particleEmitters = gameObject.GetComponentsInChildren(ParticleEmitter) as ParticleEmitter;
for (var emitters : ParticleEmitter in particleEmitters)
{
emitters.emit = false;
}
I get the following error message:
Cannot convert 'UnityEngine.ParticleEmitter' to 'UnityEngine.ParticleEmitter[]'.
Does this mean it is impossible to use an array for Particle Emitters? Or do I have something else set up incorrectly?
Answer by Mortennobel · Apr 16, 2011 at 06:24 AM
You should cast the object returned by GetComponentsInChildren to ParticleEmitter[].
Like this:
var particleEmitters : ParticleEmitter[];
particleEmitters = gameObject.GetComponentsInChildren(ParticleEmitter) as ParticleEmitter[];
for (var emitters : ParticleEmitter in particleEmitters)
{
emitters.emit = false;
}
I tried this and it compiles but now I am getting an error on the line when I try to run:
NullReferenceException: Object reference not set to an instance of an object
Does this mean it is not seeing the particle emitters?
Well, I did a check on the array and it seems like it is not grabbing my particle emitters when I am creating the array. This seems like a different problem and it seems you have solved my initial problem. Thank you!
Answer by theropodx · Dec 28, 2012 at 10:53 PM
In case this helps someone, I got this working with this code (using #pragma strict):
var particleEmitters : Component[];
particleEmitters = transform.gameObject.GetComponentsInChildren(ParticleEmitter) as Component[];
for (var emitters : Component in particleEmitters)
{
emitters.gameObject.particleEmitter.emit = true;
}