GetComponent no longer works

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.

Post the code that “no longer works”

I can assure you that the GetComponent function still works.

I’ve edited my previous post with the code

GetComponent returns a component, not a TurretAIMulti multi object

use the generic GetComponent.< TurretAIMulti>() instead

Isn’t GetComponent.() onnly used in C#?

without the point its C# syntax.
With the . in there before the < its the UnityScript generic syntax present since Unity 3.0 beta 6

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.

–Eric

I see
Something new learnt about the UnityScript compiler stealth magification in Unity 3.4, thanks :slight_smile:

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

It has nothing to do with UnityScript, it’s true for C# as well.

I don’t know the technical details, but the non-generic version is 1.6X faster, so it’s not a trivial amount.

–Eric

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.

Is there any difference with this ? :

var go = GameObject.Find("Turret");
go.GetComponent("TurretAIMulti").LoseHealth ();

just added (" ") … this works fine for me… i never tried without them…

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. :wink:

–Eric

Hmm …

Well in that case I would be even more puzzled, for 2 reasons:

  1. 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
  2. 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 :wink:

but none the less interesting to know, so I can keep an eye on code I get to see that makes use of it

I benchmarked it, it’s just plain faster. I’m more concerned with actual results than theory. :wink:

GetComponent is a Unity function, it’s not part of C#, so they can make it work however they like.

–Eric