Avoiding NullReferenceException

I’m doing a point and click game where the mouse is constantly raycasting to detect if there is an object for the player to interact with or a ‘floor’ to move into. All the other scripts in the game take into account whenever the ray hits a surface.

The game is working well and everything is like I expected but I get tons of NullReferenceException whenever the ray is not hitting any object.
I would like to know if there is a way to “catch” this issue so the debugger wont complain about it. I tried to set it to none but it will still cause issues to other scripts.

Here is the main mouse input script:

function MouseInput (){	
var playerPlane = new Plane(Vector3.up, transform.position);

	// Generate a ray from the cursor position
	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);

    // Determine the point where the cursor ray intersects the plane.
    // This will be the point that the object must look towards to be looking at the mouse.
    // Raycasting to a Plane object only gives us a distance, so we'll have to take the distance,
    //   then find the point along that ray that meets that distance.  This will be the point
    //   to look at.
	var hitdist = 0.0;
    // If the ray is parallel to the plane, Raycast will return false.
	if (playerPlane.Raycast (ray, hitdist)) {
		// Get the point along the ray that hits the calculated distance.
		var targetPoint = ray.GetPoint(hitdist);
		targetPosition = ray.GetPoint(hitdist);

		//Determine the target rotation.  This is the rotation if the transform looks at the target point.
		var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);

		// Smoothly rotate towards the target point.
		transform.rotation = targetRotation;
	}
	//transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smooth);
	
	// GETTING THE NAME OF WHAT IS DIRECTLY UNDER THE MOUSE	 - for Object selection //
    var hit : RaycastHit;
    	if (Physics.Raycast (ray, hit, 100)){
    		Debug.DrawLine (ray.origin, hit.point);		//Draws the ray
			target = hit.collider;
		
		//object the mouse is hovering on
			if (hit.transform != null){				//object the player clicks on
				hoverObject = hit.transform;
			}
			else {
				hoverObject = null;
			}
			
		//object the player clicks on							
			if (Input.GetMouseButtonUp(0) && hit.transform != null){				
				selectedObject = hit.transform;
			}
			else {
				selectedObject = null;
			}
		}
}

My main issue is the “selectedObject” that even if I set it to null, any other script that looks for it will complain when it doesnt exist, for example like this one (player_navigation is the script that contains the MouseInput):

var playerNavigation : Player_Navigation;
playerNavigation= GameObject.Find("PLAYER").GetComponent(Player_Navigation);
    
    function OnGUI () {
        
        	objIsOpen = playerNavigation.objProperties.objIsOpen;		//Is the selected Container Open or Closed?
        
        }

Any help very appreciated!

Just check for null before you reference an object that might be null.

if (object_to_reference != null)
{
   // do stuff with object_to_reference
}

If i understood your problem correctly there is no easy solution to this, but there is a dirty one:
Your other scripts should check if the selectedObject isn’t null before trying to do anything with it.
However setting flags every time you encounter such a problem may make your code messy and give room for later logic errors. (as you are having a logic error and not a syntax error right now)
My advice is to rewrite the code so you don’t have that problem.
Maybe take your objects in a list and only update them if selectedObejct isn’t null.

If you need to access one object/variable from all around the scene (eg. a bool “currentlyHasSelection”) maybe you want to look into static variables and singletons.