GuiTexture can not be displayed properly

Hello guys,

I am a new c# learner, and recently I am stuck with my first test script, I hope I can get some help here. Thanks in advance.

This is my script:

using UnityEngine;
using System.Collections;

public class touch2 :MonoBehaviour
{
	public GUITexture guitex ;
	public Ray ray;
	public void Update()
	{
		for (int i = 0; i < Input.touchCount; i++) {
			// turn on guitex
			ray = Camera.main.ScreenPointToRay (Input.GetTouch (i).position);
			if (Physics.Raycast (ray)) {


					guitex.enabled=true;
					

			}

				//turn off guitex
			if (guitex.HitTest (Input.GetTouch (i).position)) {
				
				guitex.enabled= false;
				
			}


		}
}
}

My purpose is apply this to different objects (Cube and Sphere in this case), when I touch the cube, GUItexture1 will show up, and when I touch sphere, GUItexture2 will appear. By touching on the Guitexture they will disappear accordingly.

Now the problem is when I touch either of those objects, both GUItexture is displayed, and I don’t know which part is wrong, but when I tried to use OnMouseDown method instead of ray it just work perfectly.

Your ray cast doesn’t check to see what it actually hit.

So if it hits either, its true in both components on both objects.

Look at the definition of Unity - Scripting API: RaycastHit

That returns all the information about the actual hit.