How should I use the Generic versions of GetComponent<T> and AddComponent<T>?

How should I use the Generic versions of GetComponent and AddComponent?

Fromt the `` mentioned in the title, I assume Showken is asking specifically about the Generic versions of the component commands, namely:

  • `GetComponent`
  • `AddComponent`

These are available in C# (and Unity's Javascript since Unity 3.0), and they are useful in C# because they avoid the need for the "typeof" keyword when specifying the type, and having to cast the result which is returned as an Object by the non-generic versions of these methods.

For example, without the Generic versions, these commands look like this written in C#:

Enemy enemy = (Enemy) GetComponent(typeof(Enemy));
bullet = (ProjectileScript) projectile.AddComponent(typeof(ProjectileScript));

Contrasted with the Generic versions, which look like this:

Enemy enemy = GetComponent<Enemy>();
bullet = projectile.AddComponent<ProjectileScript>();

Much nicer!

Check here for how to use "Component" in scripting: http://unity3d.com/support/documentation/ScriptReference/Component.html

as i know there is not any usecase that you might want to use it. you can create script components or create components like (Rigidbody) and call AddComponent to add the created component to a gameObject but i can not understand why you should use Component alone.