Pragma Strict - Implicit Downcast from 'Object' to 'UnityEngine.Transform'

Hey there, I’ve just been taking to my scripts with #pragma strict, and overall they’ve done pretty well in terms of an absence of instances of dynamic typing. There is one instance which is boggling me though, and that is this line:

for(var child : Transform in transform) {
	child.gameObject.layer = 8;
}

I am getting the error BCW0028: WARNING: Implicit downcast from 'Object' to 'UnityEngine.Transform'. My first thoughts were, isn’t child being typecasted to a Transform by using var child : Transform? But now I’m not so sure. I’ve tried using as Transform in various places but to no avail.

What is the correct way of typecasting this?

Thanks, Klep

AndyZ has a workaround:

for(child in transform)
{
  var typedChild : Transform = child as Transform;
  typedChild.gameobject.layer = 8;
}

I don’t know if there is any downside to it though.

That’s fine. It’s just a warning, not an error, in case you didn’t mean to do that, but I don’t think there’s a way to avoid it when doing “for (x in transform)”. You can use #pragma downcast to suppress the warning.

What works for me:

for (var i = 0; i < transform.childCount; i++)
{
  transform.GetChild(i).gameObject.layer = 8;
}

var children : Component = transform.GetComponents(Transform);
for (var child : Component in children) {
var typedChild : Transform = child as Transform;
typedChild.gameobject.layer = 8;
}

var children : Component = transform.GetComponents(Transform);
for (var child : Component in children) {
var typedChild : Transform = child;
typedChild.gameobject.layer = 8;
}

this one will work ^^