Cant get touch button to work

Here is my code for a touch button. It should make a gun shoot a ball. I can’t get it to successfully get pressed. I even changed the GUITexture x, y and such to cover to the entire screen. I ensured that it was properly given the gun rigidbody plus the ball prefeb. I just have no idea what is wrong

	public Rigidbody gun;
	public Rigidbody newball;
	public int shotspeed = 15;

	GUITexture tex;
		
	void Awake ()
	{
		tex = gameObject.GetComponent <GUITexture> ();
		tex.enabled = true;
	}
	
	
	
	
	void Update () 
	{
		if (Input.touchCount > 0) 
		{
			for(int i = 0; i < Input.touchCount; i++)
			{
				if(tex.HitTest(Input.GetTouch(i).position) && (Input.GetTouch(i).phase == TouchPhase.Began))
				{
					Rigidbody cloneball;
					cloneball = Instantiate(newball, new Vector3(gun.transform.position.x, gun.transform.position.y + 1, 0), Quaternion.identity) as Rigidbody;
					cloneball.AddForce(new Vector3(0, 1, 0) * shotspeed, ForceMode.Impulse);
				}
			}
		}
	}
}

If you’re trying to have an actual button on the screen as a HUD or so, try making the button from the UI objects. This will allow your user to hit the button and send any information from the press to a script. For the most part, Unity makes it really easy to implement button presses through the use of UI, but there are also a few other ways to make buttons procedural or through manipulation of colliders.

If you don’t know how to use the buttons, try checking out how to use the UI Unity has worked on so hard to make your life easier over the years. Button Tutorial.

To make the button work in your script, all you have to do access the script (whether it’s attached to a gameobject or not) and send in the right information.

Here’s an example that will hopefully help:

public void ShootBalls ()
{
        Rigidbody cloneball;

        cloneball = Instantiate(newball, new Vector3(gun.transform.position.x,                     gun.transform.position.y + 1, 0), Quaternion.identity) as Rigidbody;

        cloneball.AddForce(new Vector3(0, 1, 0) * shotspeed, ForceMode.Impulse);
}

Once you have that in the script and attached to the gameobject you want it on, you need the button to tell that to happen. Drag your gameobject on to the button component in the inspector under the “OnClick” and you will be able to access the “ShootBalls” function from the drop down. Select it and your button should be ready.

The code provided is just a clip from what you provided with the added function name and public access.