pragma strict convert object error

I get the following error below for `#pragma strict` - "Cannot convert 'Object' to 'UnityEngine.Transform'." How do you correctly typecast this?

o is an GameObject - defined in the function argument

function fred(o: GameObject){
  for(var child : Transform in o.transform){
    child.renderer.material = m;
  }
}   

There are no problems with your function. Try it again in a new file/project. If you have C# scripts in your project, too, the error won't go away after you fix it, until you restart Unity, unless your folders are set up properly for mixing languages.

2 Answers

2

You need to tell the compiler what o is. I assume you just have

var o;

somewhere.

You also don't need to make the transform a transform (with as). It already is one. If o can be a transform, then you can do this:

for (var child : Transform in o) child.renderer.material = m;

Otherwise, this:

for (var child : Transform in o.transform) child.renderer.material = m;

i had var o:GameObject; or rather, a function with that defined as an argument ... and i was using the latter of your snippets when I got the error above

I wish I could say why that doesn't work under strict (perhaps someone more knowledgeable will come along with that answer), however this will get your code working...

for(var childObj : Object in o.transform){
    var child : Transform = childObj as Transform;
    child.renderer.material = m;
}

That doesn't change the above, other than maybe being slower (depending on the compiler).