raycast...

My Ores, Trees and plants work fine but when i try shooting/triggering water or workbenches it doesn’t detect them…
what could be the problem?

        RaycastHit hit;			
        Vector3 fwd = transform.TransformDirection(Vector3.forward);	
		Ray ray = pCamera.ScreenPointToRay(new Vector3(200, 200, 0));		
		Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow);
		if(Physics.Raycast(pCamera.transform.position, fwd,out hit, range)) {									
			if(hit.collider.gameObject.tag == "Ore") {
				if(Input.GetButtonUp("Action")) {
	        		hit.collider.gameObject.SendMessage("CheckLevel", SM.level, SendMessageOptions.RequireReceiver);
	        		hit.collider.gameObject.SendMessage("Mine", _name, SendMessageOptions.RequireReceiver);
				}
			}
			
			if(hit.collider.tag == "Tree") {
				if(Input.GetButtonUp("Action")) {
	        		hit.collider.gameObject.SendMessage("CheckLevel", SWc.level, SendMessageOptions.RequireReceiver);
	    			hit.collider.gameObject.SendMessage("Chop", _name, SendMessageOptions.RequireReceiver);
				}
			}
			
			if(hit.collider.tag == "Plant") {
				if(Input.GetButtonUp("Action")) {
	        		hit.collider.gameObject.SendMessage("CheckLevel", SFa.level, SendMessageOptions.RequireReceiver);
					hit.collider.gameObject.SendMessage("Harvest", _name, SendMessageOptions.RequireReceiver);	
				}
			}
			
			if(hit.collider.tag == "Workbench") {
				if(Input.GetButtonUp("Action")) {
		        	hit.collider.gameObject.SendMessage("CheckCLevel", SCra.level, SendMessageOptions.RequireReceiver);
		        	hit.collider.gameObject.SendMessage("CheckSLevel", SS.level, SendMessageOptions.RequireReceiver);
					hit.collider.gameObject.SendMessage("Stone", _stone, SendMessageOptions.RequireReceiver);
					hit.collider.gameObject.SendMessage("Wood", _wood, SendMessageOptions.RequireReceiver);				
					hit.collider.gameObject.SendMessage("Activate", _name, SendMessageOptions.RequireReceiver);	
				}
			}
			
			if(hit.collider.tag == "Loot") {
				if(Input.GetButtonUp("Action")) {
					hit.collider.gameObject.SendMessage("Pickup", _name, SendMessageOptions.RequireReceiver);
				}
			}
			
			if(hit.collider.tag == "Liquid") {
				if(Input.GetButtonUp("Action")) {
					if(tool == "Fishing_Rod") {
		        		hit.collider.gameObject.SendMessage("CheckLevel", SFi.level, SendMessageOptions.RequireReceiver);
						hit.collider.gameObject.SendMessage("Fish", _name, SendMessageOptions.RequireReceiver);
					}
					
					else {
						hit.collider.gameObject.SendMessage("Drink", _name, SendMessageOptions.RequireReceiver);
					}
				}
			}
			
			if(hit.collider == null) {
				if(Input.GetButtonUp("Action")) {	
					Debug.Log("No collider found!");
					return;
				}
			}
		}

try to print(hit.collider.tag) and look what your ray finds if you shoot it @ the water, and maybe check the layer of the water

So you suggest me to check the layer instead of the tag?
It could work…

well you sound ironic, but the raycast ignores as default some layers.

Oh, I didn’t know that… :smile:

But it still doesn’t work… when I check if the layer is “Water”

spaghetti code is a little hard on the eyes.

I know this is only part of your code, but it may help you a bit to consider writing your code in a more compartmentalized sense:

As for your problem, have a look at the “GetRaycast” method.

using UnityEngine;
using System.Collections;

public class TEST1 : MonoBehaviour {

	public float range = 5;
	public GameObject pCamera;
	
	private SF SM = new SF(1);
	private SF SWc = new SF(1);
	private SF SFa = new SF(1);
	private SF SCra = new SF(1);
	private SF SS = new SF(1);
	private SF SFi = new SF(1);
	
	private string _name = "My Name";
	private string _stone = "Stone";
	private string _wood = "Wood";
	private string tool = "";
	
	private void Start(){
		pCamera = Camera.main.gameObject;
	}
	
	private void Update(){
		if(Input.GetButtonUp("Action")){
			RaycastHit hit = GetRaycast(range);
			
			if(hit.distance > 0){
				GameObject obj = hit.collider.gameObject;
				switch(hit.collider.gameObject.tag){
					case "Action":
						SendMessages(obj, "CheckLevel", "Mine", SM.level, _name);
					break;
					case "Tree":
						SendMessages(obj, "CheckLevel", "Chop", SWc.level, _name);
					break;
					case "Plant":
						SendMessages(obj, "CheckLevel", "Harvest", SFa.level, _name);
					break;
					case "Workbench":
						obj.SendMessage("CheckCLevel", SCra.level, SendMessageOptions.RequireReceiver);
						obj.SendMessage("CheckSLevel", SS.level, SendMessageOptions.RequireReceiver);
						obj.SendMessage("Stone", _stone, SendMessageOptions.RequireReceiver);
						obj.SendMessage("Wood", _wood, SendMessageOptions.RequireReceiver);			
						obj.SendMessage("Activate", _name, SendMessageOptions.RequireReceiver);
					break;
					case "Loot":
						obj.SendMessage("Pickup", _name, SendMessageOptions.RequireReceiver);
					break;
					case "Liquid":
						if(tool == "Fishing_Rod") {
							SendMessages(obj, "CheckLevel", "Fish", SFi.level, _name);
						}else{
							obj.SendMessage("Drink", _name, SendMessageOptions.RequireReceiver);
						}
					break;
					default:
					break;
				}
			} else {
				Debug.Log("No collider found!");
				return;
			}
		}
	}
	
	private RaycastHit GetRaycast(float range){
		// camera center
		Ray ray = new Ray(pCamera.transform.position, pCamera.transform.forward);
		// some other object?
		//Ray ray = new Ray(pCamera.transform.position, transform.position);
		
		RaycastHit hit = new RaycastHit();
		if(Physics.Raycast(ray, out hit, range)){
			Debug.DrawLine(ray.GetPoint(0), ray.GetPoint(hit.distance), Color.yellow);
			Debug.DrawLine(hit.point, hit.point + hit.normal, Color.red);
			Debug.Log(hit.collider.gameObject.tag);
		}
		return hit;
	}
	
	private void SendMessages(GameObject obj, string name1, string name2, float level, string name3){
		obj.SendMessage(name1, level, SendMessageOptions.RequireReceiver);
		obj.SendMessage(name2, name3, SendMessageOptions.RequireReceiver);
	}
}

public class SF{
	public float level;
	
	public SF(float level){
		this.level = level;
	}
}

Well, Everything works now and much smoother… thank you!