I have spent the last few hours trying to upgrade my project to work with Unity 3.4 and from what I have heard the GetComponent function no longer works.
For example I have:
var go = GameObject.Find("Turret");
go.GetComponent(TurretAIMulti).LoseHealth ();
If anyone can help me get around this problem then I would be very grateful.
Not in 3.4 it doesn’t. GetComponent(TurretAIMulti) doesn’t return Component anymore, it returns TurretAIMulti now.
Not necessary in 3.4, and in fact I’d suggest using GetComponent(TurretAIMulti) rather than GetComponent.(), since it’s faster than the generic version.
I tried this with a dummy TurretAIMulti script that has a LoseHealth function, and it works perfectly fine, so this code isn’t the problem.
I see
Something new learnt about the UnityScript compiler stealth magification in Unity 3.4, thanks
Though I don’t see why it should be faster if it does magi-cast again, as typecasting is typecasting independent on if the compiler hides it or not, its only the end user thats fooled
That would be new to me as automatic type inference normally only works with the variable datatype, which I will not touch even if hell freezes (I want to know what I look at and var datatypes don’t tell me anything without searching where they are initialized or having the IDE telling me), but I will test if you say that GetComponent(typeof(MyClass)) should newly return myclass, not Component.
That’s not a good idea. Using quotes is slower, and typos are only caught at run-time, not compile-time. It’s better if you always stay away from using quotes in GetComponent.
Yes, because it’s only true starting with Unity 3.4, which was just released.
Well in that case I would be even more puzzled, for 2 reasons:
you mention its faster, but if it works in C# and complies to the standard it would be the normal type inference which would actually fall through from GetComponent(typeof(…)) to GetComponent<…>() ( as per http://msdn.microsoft.com/en-us/library/ms364047(v=vs.80).aspx extension method invocation) in which case it would no longer be faster
We talk about C# here, a global standard, not a language that can be toyed with as desired, for that purpose Boo and UnityScript already exist, at least one language should hold to a standard that does not change yearly
but none the less interesting to know, so I can keep an eye on code I get to see that makes use of it