Raycast cant find "Location" tagged objects..

I’m new to Javascript and i am trying to create a game for a puzzle game for a University project. The user has to drag a object(puzzle piece) over another(Location) and there is a raycast to check to see if it in the right location. The problem is that it’s not finding the location.

Whats the problem, because i cant see it…


function Drop()
{  
    AmDragging = false;
    
    var ray = Camera.mainCamera.ScreenPointToRay (Input.mousePosition);
    var hit : RaycastHit;

    if (Physics.Raycast(ray, hit, 1000)) {
		var hitTag = hit.collider.gameObject.tag;
		Debug.Log ("Value of hitTag ="+hitTag);
        if (hitTag == "Location")
        {
        	     var locationHit = hit.collider.gameObject.name;	
        	     Debug.Log ("Location hit = "+locationHit);
        }
    	else
    	{
    		Debug.Log ("Not hit location");
    	}
	}
}

I’d keep it simple, try not using pointless vars to keep it nice and neat.

function Drop() {  
	AmDragging = false;
    	var ray = Camera.mainCamera.ScreenPointToRay (Input.mousePosition);
    	var hit : RaycastHit;

    	if (Physics.Raycast(ray, hit, 1000)) {
		if (hit.collider.tag == "Location"){
			Debug.Log ("Location hit!");
        	} else {
			Debug.Log ("Not hit location");
    		}
	}
}

Also, is there even a tag attached to your Object? It has to be something with a collider/trigger and you can turn off the mesh renderer so it’s “invisible”.

Good luck.

You should be more specific about what you’re getting with this script. Is the “Value of hitTag” message being printed? If so, what’s being printed?

There are also other few things you could check:

1- Does the Location object have a collider? Raycast only hits colliders.

2- Is the Location collider being physically obscured by another collider? Only the first collider hit is reported by Raycast, thus any object that’s in front of Location may obscure it (even Location children).

3- Is the Drop function being called?