Instantiate at hit.point

Hello all!

I’ve been trying to instantiate a gameobject at the mouse position. I know to do this I need to use RaycastHit, but when I try to instantiate with Instantiate(waypoint, hit.point, Quaternion.identity);, it doesn’t work. The console doen’t say that there are any errors, but when I try to use the script, nothing happens. Any ideas?

Full script:

#pragma strict

var waypoint: GameObject;

function Update () {
		if (Input.GetMouseButtonDown(0)) {
			var hit: RaycastHit;
			var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			
			if (Physics.Raycast(ray, hit)) {
				if (hit.rigidbody != null)
					Instantiate(waypoint, hit.point, Quaternion.identity);
				}
		}
}

Your code requires that the hit object have a rigid body but many objects only have colliders. Are you sure that you want to limit this hit to rigid bodies only?

Thanks all! I changed it to if (hit.collider != null) and that solved the problem.