C# change an object tag wit raycasthit.

hi I’m new to unity and I’m building an ios game in C#. all I would like to do is toggle a game object tag with touch input. I’m using a raycast (I got the script form another thread). I managed to get it working with a send message but have since found out that they are a bad habit to get in to. I have tried this but now touch inputs do nothing what so ever. I would really appreciate any advice or solutions. Is there an easier way to do this? Have I miss understood the whole situation? Thanks in advance.

void Update () {
       TapSelect(); 
}
 
void TapSelect() {
   foreach (Touch touch in Input.touches) {
        if (touch.phase == TouchPhase.Began) {
            Ray ray = Camera.main.ScreenPointToRay(touch.position);
            RaycastHit hit ;
            if (Physics.Raycast (ray, out hit)) {
                       if(hit.collider.tag == "objectOff")
		               { 
						hit.collider.tag = "objectOn";
		               }
					    else {
	                     hit.collider.tag = "objectOff";
		              }	
				   }
				}
            }
        }
    }

thanks for the responses. it’s attached to a sphere collider. and I can’t run debug.log because I’m testing on an iPhone so as far as I’m aware I can’t see the log. however I have just realised that is is detecting touch just off to the side away from the game object. why would that be? any ideas, can I re-center the detection?

your doing

Camera.main.ScreenPointToRay(touch.position)

are you sure touch.position is a screenpoint?

A screenpoint is the pixel location but perhaps touch.position is a world position so its distance from the global origin not its screen position.

If so the use of

camera.main.ScreenPointToRay(camera.main.WorldToScreenPoint(touch.position));

may be the appropriate way to solve this.

My guess is basically that touch.position is a viewport point or a world point and not a screenpoint.

//first you need to write down both tag in tag manager

private Ray ray;
private RaycastHit hit;

void Update()
{
	ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		 
	if(Physics.Raycast(ray,out hit, Mathf.Infinity) && Input.GetMouseButtonDown (0))
	{
		
		if(hit.collider.gameObject.tag == "Wood")
		{
			hit.collider.gameObject.tag="Water";
		}
	}
}