Why do I see people using GetComponent<name>, and how is it different from GetComponent(name)?
I am sure this has been asked but I can’t figure how to properly ask the question since I don’t really understand what <> does.
mj321
2
GetComponent has three overloads:
public T GetComponent<T>();
public Component GetComponent(string type);
public Component GetComponent(Type type);
The main difference for the <> version is that it already returns the correct type while the other two versions return a Component that needs to be casted to the correct type.
These three lines taken from the documentation do the same:
HingeJoint hinge = gameObject.GetComponent<HingeJoint>( );
HingeJoint hinge = gameObject.GetComponent( "HingeJoint" ) as HingeJoint;
HingeJoint hinge = gameObject.GetComponent( typeof(HingeJoint) ) as HingeJoint;
As for the <>: Look up “C# generics”