- Home /
Casting error on multiple emitters
function Start()
{
var emitters : ParticleEmitter[];
emitters = GetComponentsInChildren(ParticleEmitter);
for (var emitter: ParticleEmitter in emitters)
{
emitter.emit = false;
}
}
I am trying to turn a set of emitters on and off. When I use this script I get "Cannot cast from source type to destination type."
Answer by Justin Warner · Dec 29, 2010 at 08:23 PM
function Start() {
var emitters : ParticleEmitter[];
emitters = GetComponentsInChildren(ParticleEmitter);
for (var emitter: ParticleEmitter in emitters)
{
emitter.emit = false;
}
}
Rather .emit, use .enable maybe, but I'm mostly just cleaning up your code and putting it in tags...
Sorry the code formatting didn't come through, but that was a totally useless reply. Emit is true or false and the error is in the Get Components line.
Answer by Molix · Apr 15, 2011 at 06:22 PM
GetComponentsInChildren returns a Component[], so you need to get them as that, then cast each to ParticleEmitter, e.g.
var emitters: Component[];
emitters = GetComponentsInChildren(ParticleEmitter);
for( var comp : Component in emitters )
{
ParticleEmitter emitter = (ParticleEmitter)comp;
emitter.emit = false;
}
Answer by Mr_T · Jan 29, 2011 at 05:02 AM
I ended up fixing this by targeting the individual game objects instead of trying to target them as a group using the for loop. I still don't understand why I can't target them as components in children