loop through children strongly typed

I started using #pragma strict and have the error cant convert object to transform, how would i loop through the children of a transform without errors strongly typed?

var thing:Transform;

Function Start(){
    for(var child:Transform in thing){
        //do stuff
    }
}

the only error I see is that function should be a lower case 'f'. What errors are you getting?

While using #pragma strict i was getting Cannot convert 'Object' to 'UnityEngine.Transform', when i was going through a transforms children with a 'for in' loop, that kind of for loop seemed to return Objects, not transforms, when iterating through the children. I think that has to do with how a transform stores its children; as enumerations er something i think, that's where the documentation info i could find stopped.

I never implied you were stupid. I just tried to understand the system you're implementing because I didn't make sense for me (simply because I didn't have the whole context). But even with your explanation, I also share logicandchaos' point of view. I believe you're taking the wrong path.

1 Answer

1

I found using ‘as Transform’ like

 var stuff:Transform=GetStuff() as Transform;

can settle most of the #pragma strict errors.

for looping through children though, what i found works is to:

 var thingsChildList:Transform[]=thing.GetComponentsInChildren(Transform) as Transform[];
 for(var child:Transform in thingsChildList){
    //do stuff
 }

also, GetComponnentsInChildren will return the component in the object your searching from, so you might have to add an exception excluding itself from any actions taken on the children: if(child!=thing)//do stuff anyone found another way of looking through the children?

Instead of using GetComponent( Type ) which returns Component and you will then typecast as Type GetComponentsInChildren( Transform ) as Transform; use the generic version which returns the actual type. This function is nearly always better GetComponentsInChildren.< Transform >()