Getting the object's name from RaycastHit

Hi there, I am trying to get which object I am hitting with my ray. Eventually I want to call a function inside that object's script. as in you fire at a barrel - barrel has a BOOM function in it's script - if the barrel is hit, BOOM gets called.

I think I got the raycasting down:

var ray =Camera.main.ScreenPointToRay(new Vector3(Screen.width/2,Screen.height/2));
    var hit : RaycastHit;
    	if (Physics.Raycast (ray, hit, 1000)) {

    	print(hit.collider); //oh yeah, I am hitting stuff such as, UnityEngine.MeshCollider

    	Debug.DrawLine (ray.origin, hit.point);//I can also see that the rays work, awesome.

Now I have tried things like hit.BOOM which is of course not working :)

Thanks in advance :)

Once you have determined that your raycast actually hit something, you can query your 'hit' object for many different properties.

For the gameObject's name:;

For the object's tag:


And there are many others - see the RaycastHit manual page.

Because "collider" is a component, you can use "GetComponent" on the hit.collider result, to get access to any other component on the gameObject - including your own script.

You would use something like this (inside your , assuming your script is called "BoomScript" and it has a function in it called "Boom":

if (Physics.Raycast (ray, hit, 1000)) {
    var boomScript : BoomScript = hit.collider.GetComponent(BoomScript);
    if (boomScript != null) {