raycast not working for me

hi im trying to do a simple raycast hit on a collider, but can not seem to get it to work all i get is null reference, ive looked at about twenty answers on here but they all do the same, any help

You can attach a script to your collider which has the following function:

void OnMouseDown()
{
//Do something
}

Check Unity - Scripting API: MonoBehaviour.OnMouseDown() for more details.

This way you don’t need to do the raycasting yourself.

Cheers

You can get alot more information from the hit of a raycast, and even send message.

Try this. Make a new script, then paste the below code. Attach the script to any gameObject, then hit play. If you objects have colliders, you should see their name being displayed in the console window when you click on them.

function Update (){
    if (Input.GetMouseButtonDown(0)){
        var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        var hit : RaycastHit;
    if (Physics.Raycast (ray, hit)) {
            Debug.Log(" You just hit " + hit.collider.gameObject.name);
        }
    }
}