Object reference not set to an instance of an object (with a RaycastHit)

I’m trying to destroy an object when I right click, and the game isn’t paused, and the object my raycast collides with has the tag of “item”. When I do this I get the error in the title about hit.transform.gameObject.tag == “item”. Here’s all of the code,

var cube : GameObject;

var cubeClone : GameObject;

var moveFromPoint : float = 2;

var atoms : float = 1000;

@HideInInspector var paused : boolean;

@HideInInspector var tooClose : boolean;

function Update () {
	
	paused = Camera.main.GetComponent(FPSCamera).paused;
	
	//test to see if you are too close to a wall
	var hit : RaycastHit;
	if(Physics.Raycast(transform.position, transform.forward, 2))
			tooClose = true;
				else
					tooClose = false;
	
	//test to see if you can place cube	
	if(Input.GetButtonDown("Fire1") && tooClose == false && atoms >= 100 && paused == false){
		cubeClone =	Instantiate(cube, transform.position + transform.forward * moveFromPoint, Quaternion.Euler(0,0,0));
				atoms = atoms - 100;
	}
	//test to see if you can remove
	if(Input.GetButtonDown("Fire2") && paused == false && hit.transform.gameObject.tag == "item"){
	Debug.Log("it works!");
	}
}

the last if statement provides this error when I right click the “item”, Object reference not set to an instance of an object. Which is referencing the hit.transform.gameObject.tag but I’m not sure why.

I dont think so ‘hit’ will automagically get assigned without being passed as an argument in Physics.Raycast() :D. Use another overloaded method for Physics.Raycast() that accepts RayCastHit as an argument.

Hi, you need to pass the instance of RaycastHit “hit” to the method Physics.Raycast to get the hit informations you like to have. In your code you only check if something at the range of 2 get’s hit and that’s it. Your “hit” variable is still empty.

Here is the example of the Unity Script Reference:

  function Update () {
         var hit : RaycastHit;

        if (Physics.Raycast (transform.position, -Vector3.up, hit, 100.0)) {

            var distanceToGround = hit.distance;
        }
    }

Hope that helps.

Greetings.
Christian

You need to pass the ‘hit’ to the Raycast() for it to be initialized, but even if you do that, at the point you want to use the ‘hit’ you don’t know if it is initialized or not. You need to restructure your code. Here is a bit of a rewrite (untested):

var cube : GameObject;
var cubeClone : GameObject;
var moveFromPoint : float = 2;
var atoms : float = 1000; 
 
function Update () {
 
    if (Camera.main.GetComponent(FPSCamera).paused)  // Just bail out if the game is paused
    	return;

    //test to see if you can place cube    
    if(Input.GetButtonDown("Fire1") && atoms >= 100 ){
       	if(!Physics.Raycast(transform.position, transform.forward, 2)) {  // Note the '!'
       		cubeClone =   Instantiate(cube, transform.position + transform.forward * moveFromPoint, Quaternion.identity);
          	atoms = atoms - 100;
         }
    }
    //test to see if you can remove
    if(Input.GetButtonDown("Fire2")){
    	var hit : RaycastHit;
    	if(Physics.Raycast(transform.position, transform.forward, hit, 2)) {
    		if (hit.transform.gameObject.tag == "item") {
    			Debug.Log("it works!");
    		}
    	
    	}
    }
}