Make object block a raycast

I am using raycast to check for touches on object, however the problem I have is that the raycast is passing thru walls and activating the touch code on objects on the other side of the wall. For example on one side of the wall there is a torch and on the other is a chest. WWhen player touches the torch the chest on the other side opens.

Is there a way to stop raycasts from passing thru certain objects?

On Torch objects the code is

function Update ()
{
	if (Input.GetMouseButtonDown(0)) // check for left-mouse
	{
		if (torchLit == false){
			var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
			var hit : RaycastHit;
			if (collider && collider.Raycast (ray, hit, 100.0))
			{
				if(hit.collider.tag == "Torch"){
					OnMouseDown();
				}
			}
		}
	}
}

And on my chest object

function Update () {
	if (Input.GetMouseButtonDown(0)) // check for left-mouse
	{
		var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		var hit : RaycastHit;
		if (collider && collider.Raycast (ray, hit, 100.0))
		{
			if(hit.collider.tag == "chest"){
				OnMouseDown();
			}
		}
	}
	if (chestOpen == true){
		ChestOpened();
	}
}

Thanks in advance.

You are using collider.Raycast, which in the docs says specifically that it “Casts a Ray that ignores all Colliders except this one.”

What you would need to do is in some other Update function in a master script somewhere (so that it is only running once), use Physics.Raycast. In all the other objects just leave the OnMouseDown function that you have already, and remove the raycasts from their Update functions. This avoids the Raycasting from multiple objects each ignoring all other objects, and gives you one ray cast that will stop at the first object hit.

//in some game controller script
function Update () {
    if (Input.GetMouseButtonDown(0))
    {
        var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        var hit : RaycastHit;
        if (Physics.Raycast (ray, hit, 100.0))
        {
            hit.collider.gameObject.SendMessage ("OnMouseDown", null, SendMessageOptions.DontRequireReceiver );
        }
    }
}

Cheers!