Moving a player with GUI Texture

I have a GUI Texture and the following script is attach to it.

using UnityEngine;

using System.Collections;

public class moveUP : MonoBehaviour {

public float moveSpeed=3f;
public GameObject player;
void OnGUI() {
	   if (Input.GetMouseButton(0))
		player.transform.Translate(Vector3.up * moveSpeed * Time.deltaTime);

}

It works not only when I click the GUI Texture, but also whereever I click on the screen.

How can I fix this?

Or if you have any other script for moving a player with GUI Texture feel free to share it.

If you have a reference to the GUITexture (probably populated via the Inspector), you can call its HitTest function to check if a particular screen point is “in” it.

Which point would you check? Probably Input.mousePosition.

So, putting it all together:

//assumes you have a GUITexture variable named "myTexture"
if (Input.GetMouseButton(0) && myTexture.HitTest(Input.mousePosition)) {
    player.transform.Translate(Vector3.up * moveSpeed * Time.deltaTime);
}