I can't understand the syntax of GetComponent() in C#

I have a script that look like this:

var damage:float = 5;
var attackThisTag:String = "enemy";

function OnCollisionEnter(collision : Collision) {

    if((collision.gameObject.GetComponent("damageControl") != null) && (collision.transform.root.tag == attackThisTag)){



I don't know how to do the same in C#. I want to check if the damageControl script exist on the collision object and the tag of the object. And if the script exist to call the MakeDamage() function in the script. I can't understand the help on GetComponent in C# example. Thanks in advance.

As given in the script reference for GetComponent, there are two ways to use it.

T foo = GetComponent<T>();
T foo = GetComponent("T") as T;

For your specific code, it would be:

if((collision.gameObject.GetComponent<damageControl>() != null) && 
   (collision.transform.root.tag == attackThisTag))


if(((collision.gameObject.GetComponent("damageControl") as damageControl) 
   != null) && (collision.transform.root.tag == attackThisTag))

There are several things to be concerned with in this script that might make your life easier:

  • You should store the component that you get rather than calling GetComponent twice.
  • As Mike says, rather than getting the component, you could just do something like collision.gameObject.SendMessage("MakeDamage", damage); and avoid the if statement entirely. This would add some overhead for messaging, but would also go across languages more easily and wouldn't require a component reference.

Use GetComponent() instead - it's the generic version which returns the correct type instead of a less helpful Component reference

This is assuming that damageControl is in c#, otherwise things get a lot messier, and you'll probably just want to use SendMessage instead

I can't remember what versions of GetComponent() are available in C#, but I always use the generic version, i.e.:

damageControl damageControlComponent =

Note that it would be a little more efficient to acquire a reference to the component (if it exists) once, as shown above, and then test the reference against null in your conditional.

[Edit: Too slow X 2...]

The answers above supply more than sufficient information , all I can add for you might be that you should always be carefull in game design to check that when getting a component. It simply may not be there, and during runtime that can cause some serious crash errors. Always encapsulate getting components by stuffing it into testboxes.

    //Get what u need
catch (Missing....Exception e)
    //report the problem code (Can't post the code sorry!)

and redundancy checks wont kill you either

if (/*the component I just got*/ != null)
if (!component) //Short version

You can even set a bool to check if something exists but that's even more overkill.

Whenever you handle these exception it's robust programming , and notify the console where the problem occured or what problem , preferably both. Can save you the insane amount of debugging time you will be spending later , as is common in development, it IS the bottleneck of the production line after all.

EDIT: 11/1/2010

This sample checks if there is a main camera of my own code, if not it reports back the place where the problem is to the console and sends the object with it to which this code is attached.

if (OBJECT/VAR == null) ////////Can also do if (!OBJECT/VAR) { ... }

This sample shows a more simple GetComponent check for a an object, it will assign only if it's not null but will not report anything if it fails ( which probably is not a great idea )

if (GetComponent<CharacterController>() != null)
    controller = GetComponent<CharacterController>();            

This sample shows the try catch block


Maybe they can explain it better than I can :)

Good luck with your coding, hope this helps, feel free to ask more!