- Home /
Question by
Orloffyeah · Jun 23, 2013 at 04:20 AM ·
guitexturemouseclickdetect
Detect Mouse Click in a GUITexture
Hi, currently I managed to put together a simple script that verifies if the player wants to exit the game, and attached it to a GUITexture, but the problem is that wherever I click in the screen, the script starts working. How can I make that it only works when I click the GUITexture?
Verification Code:
public class Verification : MonoBehaviour {
private bool exitClicked = false;
void Update()
{
if(Input.GetMouseButtonDown(0))
{
exitClicked = true;
}
}
void OnGUI ()
{
if(exitClicked == true)
{
GUI.Label(new Rect(Screen.width/2 - 30, Screen.height/2 - 50, 100, 20), "Are You Sure?");
if (GUI.Button (new Rect (Screen.width/2 - 100,Screen.height/2,100,60), "Yes"))
{
Application.Quit();
}
if (GUI.Button (new Rect (Screen.width/2 + 20,Screen.height/2,100,60), "No"))
{
exitClicked = false;
}
}
}
}
Comment
Best Answer
Answer by kubci98 · Jun 23, 2013 at 07:42 AM
In void Update, you switch exitClicked to true everytime, when user press mouse button 0. It doesn't depend where pointer is when he press it. You should use OnMouseDown function.