Object reference not set to an instance of an object? (86965)

I have the error “Object reference not set to an instance of an object”. My script:

function Update () 
{
	var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	var hit : RaycastHit;
	
	if (Physics.Raycast (ray)) 
	{
		**if(hit.collider.gameObject.tag == ("RecomendedMenu"))
		{
			RecomendedMenu.renderer.enabled = true;		
		}
		
		if(hit.collider.gameObject.tag == ("KojiMenu"))
		{
			KojiMenu.renderer.enabled = true;		
		}**
	}
}

The “tag” parts of my script are the ones that are getting the error. “RecomendedMenu” is the correct tag and the variable “RecomendedMenu” has been declared. What is wrong?

1 Answer

1

The problem is that your not assigning any information to hit, it remains null, resulting in the error when you try to use it. Changing line 6 to:

 if (Physics.Raycast (ray, hit)) 

should fix that.

Oh yeah, I forgot to put hit in! Thanks!