Yes, sorry about that. I was under the impression, that (with generics now supported in JS) the generic Unity functions would also work for JS. Apparently I was wrong. Perhaps someone with more hands-on JS experience knows why it doesn’t work.
Seems to me the as ScriptName is quite abdundant, since 99% of the time it’s the script you want, not the Component class… It’s strange though that it seems like sometimes you need it and sometimes you dont…
No, it still works fine in Unity 3. The exception is if you’re using #pragma strict. In which case, you can add #pragma downcast and then it will allow downcasting like Unity 2.6 did.
The correct syntax in JS is:
var menuElement = gameObject.GetComponent.<MenuElement>();
You could do “var menuElement : MenuElement” if you want to be 100% explicit, but it seems a bit redundant here.
That’s why you add “as ScriptName”, since GetComponent returns Component, so you have to cast it as ScriptName. Unless you’re using dynamic typing or #pragma downcast.
Not actually strange…if you’re using dynamic typing or #pragma downcast, you don’t need it. Otherwise you do.
Maybe I’m just being superstitious, but I have found that with #pragma strict, I’ve had to both declare the variable AND use the at:
var newItem : Classname = gameObject.GetComponent(Classname) as Classname;
Seems pretty redundant, and Eric suggests not all those are needed, but I’ve had a hard time getting some of the scripts to work without doing both. Is that crazy?
However, this was a hang up that I burned up a couple hours trying to figure out. I find myself fearful that there will be many other “hang ups” related to using #pragma strict that will also be remarkably time consuming to figure out.
Can anyone else provide insight into whether there will only be a couple other hang ups or if there will be a laundry list of difficulties just like this one?
I am thinking for the sake of productivity I may just go back to dynamic typing.