- Home /
Typecast a Transform During a for Loop
Pretty sure there's an easy answer to this.
I'm trying to figure out how to typecast the following but Unity keeps telling me I can't convert and Object to a Transform. It is of course automatically downcasting but I want to explicitly set it.
Here's my original source code:
for(var _child : Component in transform ) {
if(_child.particleEmitter)
_child.particleEmitter.emit = true;
}
And here are all the variations I've tried but with no success
for(var _child : Transform in transform) {
if(_child.particleEmitter)
_child.particleEmitter.emit = true;
}
for(var _child : Transform in transform as Transform) {
if(_child.particleEmitter)
_child.particleEmitter.emit = true;
}
for(var _child : Component in transform as Component) {
if(_child.particleEmitter)
_child.particleEmitter.emit = true;
}
for(var _child : Component in transform) {
if(_child.particleEmitter)
_child.particleEmitter.emit = true;
}
var _tempTransform : Transform = transform as Transform;
for(var _child : Transform in _tempTransform) {
if(_child.particleEmitter)
_child.particleEmitter.emit = true;
}
var _tempTransform : Component = transform as Component;
for(var _child : Component in _tempTransform) {
if(_child.particleEmitter)
_child.particleEmitter.emit = true;
}
var _tempTransform : Transform = GetComponent(typeof(Transform)) as Transform;
for(var _child : Transform in _tempTransform) {
if(_child.particleEmitter)
_child.particleEmitter.emit = true;
}
Answer by Eric5h5 · Oct 25, 2013 at 03:57 AM
for (t in transform) {
var child = t as Transform;
if (child.particleEmitter)
child.particleEmitter.emit = true;
}
Thanks a bunch Eric5h5. That worked.
As an aside, I've always set the type in my for loop parameters (var t : Transform in transform). I didn't realize you didn't need to do that.
$$anonymous$$arked as answered. :)
The type of the variable in the loop is picked up from whatever you're iterating through, so it's not necessary to explicitly specify it. In this particular case, though, iterating over transform returns Object; as Bunny83 mentions, it's untyped.
Answer by aldonaletto · Oct 25, 2013 at 01:53 AM
This alternative (similar to the one used in the docs) should work fine:
for(var _child : Transform in transform) {
if(_child.particleEmitter)
_child.particleEmitter.emit = true;
}
From the error message I suspect that you have an untyped variable called transform anywhere in your script, and it's hiding the property transform. If you can't find this mysterious variable, try to change the code to this:
for(var _child : Transform in gameObject.transform) {
if(_child.particleEmitter)
_child.particleEmitter.emit = true;
}
Is it even possible to loop over a gameObject.transform?
Yes, it's possible since the Transform class implements the IEnumerable interface. Sadly only the untyped version so you have to specify the element type in the for loop.
Thanks aldonaletto. I tried that but it still gave me the same message.
I don't have a variable named transform.
Your answer
![](https://koobas.hobune.stream/wayback/20220613120603im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Make compiler treat an object variable as if it was of type X 2 Answers
Problem typecasting System.Object to boo.lang.hash in javascript 0 Answers
Cannot cast from source type to destination type 2 Answers
Change what script a var holds? 1 Answer
How to properly check serializedProperty.objectReferenceValue for null? 2 Answers