Getting game object from RayCast

Hello,

I’m trying to get a gameObject from a raycast but i get an NullReferenceError on the second line (I got this code from the scripting reference for editing it later).

if(Input.GetKeyUp(KeyCode.Mouse0)) {
    var ray = Camera.main.ScreenPointToRay (Input.mousePosition); // < Error?
    var hit : RaycastHit;
    if (Physics.Raycast (ray, hit, 100)) {
        Debug.DrawLine (ray.origin, hit.point); 
    }
}

http://forum.unity3d.com/threads/38646-ScreenPointToRay-MousePosition

That should be pretty helpful to you. But my guess is that you do not have a camera tagged as “MainCamera”.