- Home /
Question by
techno9487 · Nov 03, 2013 at 01:34 PM ·
nullreferenceexceptionnull
Null Reference Raycast
So i have this code:
using UnityEngine; using System.Collections;
public class RaycastExample : MonoBehaviour {
void Update () {
if (Input.GetMouseButtonDown (0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Debug.Log ("Name = " + hit.collider.name);
Debug.Log ("Tag = " + hit.collider.tag);
Debug.Log ("Hit Point = " + hit.point);
Debug.Log ("Normal = " + hit.normal);
Debug.Log ("Object position = " + hit.collider.gameObject.transform.position);
Rigidbody rb = hit.collider.rigidbody;
if (rb != null)
Debug.Log ("Velocity = " + rb.velocity);
Debug.Log ("--------------");
}
}
}
}
and this line "Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);" is causing a null reference exception, and i can't work out why
Comment
Does your camera have this tag? ''$$anonymous$$ainCamera" ?