- Home /
Unityscript - Using "as" to cast for GetComponentsInChildren returns empty array
I have an instantiated GameObject with multiple ParticleSystems on it (each on a GameObject that is a child of the main GameObject). To stop the entire thing from emitting further particles, I use this:
function StopEmission(theParticleObject : GameObject) {
var tempComponents : Component[];
tempComponents = theParticleObject.GetComponentsInChildren(ParticleSystem);
for (var particle : ParticleSystem in tempComponents) {
particle.enableEmission = false;
}
}
However this generates an "implicit downcast" warning. I understand I should be able to cast out the error as follows:
var tempComponents : ParticleSystem[] = theParticleObject.GetComponentsInChildren(ParticleSystem) as ParticleSystem[];
for (var particle : ParticleSystem in tempComponents) {
particle.enableEmission = false;
}
However, the for loop in this case never returns any results and I'm not sure why. I believe "`as`" will return null when the casting fails, but why shouldn't this work?
EDIT: This doesn't work either, harrumph:
var tempComponents : Component[];
tempComponents = theParticleObject.GetComponentsInChildren(ParticleSystem);
for (var particle : ParticleSystem in tempComponents as ParticleSystem) {
particle.enableEmission = false;
}
Not sure if this will help as I've never done anything like this.
The first example looks fine except for
var tempComponents : Component[];
You should start by using the first example, but change that line to
var tempComponents : ParticleSystem[];
When using GetComponent in uJS you can declare the parameter as a string or as a component type
theParticleObject.GetComponentsInChildren( ParticleSystem );
theParticleObject.GetComponentsInChildren( "ParticleSystem" ) as ParticleSystem;
you only need to append the as when you are using the string format.
For your second example, I think it is falling down because of the typecasting you use
var tempComponents : ParticleSystem[] = theParticleObject.GetComponentsInChildren(ParticleSystem) as ParticleSystem[];
should be
var tempComponents : ParticleSystem[] = theParticleObject.GetComponentsInChildren(ParticleSystem) as ParticleSystem;
though I could be very wrong about that!
You are populating an array of ParticleSystems, but each component is just a ParticleSystem.
Again the same rule applies : if you are not declaring the parameter as a string then you don't need to use as :
var tempComponents : ParticleSystem[] = theParticleObject.GetComponentsInChildren( ParticleSystem );
Finally you can remove any downcast warnings by using
#pragma downcast
but personally I never do this myself, it just masks the issues you have, I really don't know if pragma downcast fixes the problems on building, but in C# you definitely don't get away with incorrect typecasting =]
Downcasting isn't a problem, it's just a warning that you might not have intended it.
Aah, thankyou, that is great information. I was holding my breath after your answer, wondering if and what I got wrong.
Oh, and you would want to type as ParticleSystem[] rather than ParticleSystem (or rather, you would if that worked); it's an array, so trying to cast an array as a not-array would result in an error.
Also, the casting doesn't really have anything to do with strings; GetComponentsInChildren() always returns Component[] regardless. So the casting doesn't work for whatever reason. That's why it's better to use GetComponentsInChildren.(), since that does cast as an array of the correct type.
Answer by Eric5h5 · Apr 25, 2013 at 07:48 PM
Use the generic version of GetComponentsInChildren; that way the array is automatically cast to ParticleSystem:
function StopEmission (theParticleObject : GameObject) {
var tempComponents = theParticleObject.GetComponentsInChildren.<ParticleSystem>();
for (var particle in tempComponents) {
particle.enableEmission = false;
}
}
Bingo! That did it; thanks Eric. Why does GetComponentsInChildren(ParticleSystem)
exist if only GetComponentsInChildren.ParticleSystem>();
works "properly?"
GetComponentsInChildren.() was added later. The non-generic version does work properly, just not conveniently. ;) (I rarely ever want to deal with Component.)