- Home /
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.
Answer by Jessy · May 09, 2011 at 02:30 AM
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
Answer by loramaru · May 09, 2011 at 06:51 AM
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).
I doesn't change it functionally but it addresses the error induced by the strict pragma. I'm not sure why the iterator on GameObject.transform seems to return Objects rather than Transforms; but that is the behavior that ina was seeing and it's what I see in Unity as well. Perhaps this is intentional, a quirk of $$anonymous$$ono that I don't know yet, or even a Unity bug; regardless the explicit cast appears needed with the strict pragma.
Your answer
Follow this Question
Related Questions
object not set to instance..........again. 2 Answers
pragma strict issues with iOS 1 Answer
Global Variable[Solved] 1 Answer
Move Object to location of Trigger? 1 Answer