- Home /
Detect Mouse Click on GUI Texture (NOT ONGUI)
Hi everyone, I am new to Unity, and Unity Answers.
I am creating a stat selection screen for homework. I am confused about writing a line of code that is probably very simple. I want to detect a click on the GUI Texture without using on Gui if its possible. :)
I have searched for the post that can help me but I do not think anyone is in my situation.
I hope somebody can help! :) Thanks for your time.
using UnityEngine;
using System.Collections;
public class StatScript : MonoBehaviour {
private int[] stats = new int[] {5,5,5,5,5,5};
private int pointSpend = 25;
public GUIText points;
public GUIText strength;
public GUITexture strengthSpend;
public GUITexture strengthReturn;
public GUIText stamina;
public GUITexture staminaSpend;
public GUITexture staminaReturn;
public GUIText constitution;
public GUITexture constitutionSpend;
public GUITexture constitutionReturn;
public GUIText agility;
public GUITexture agilitySpend;
public GUITexture agilityReturn;
public GUIText intellegence;
public GUITexture intelligenceSpend;
public GUITexture intellegenceReturn;
public GUIText willpower;
public GUITexture willpowerSpend;
public GUITexture willpowerReturn;
public AudioClip selectTone;
// Update is called once per frame
void Update () {
points.text = pointSpend.ToString ();
strength.text = stats [0].ToString ();
stamina.text = stats [1].ToString ();
constitution.text = stats [2].ToString ();
agility.text = stats [3].ToString ();
intellegence.text = stats [4].ToString ();
willpower.text = stats [5].ToString ();
}
void OnMouseUp(){
audio.PlayOneShot (selectTone);
if (strengthSpend)){
stats[0]= stats[0] +1;
pointSpend = pointSpend -1;
}
else if (strengthReturn){
stats[0]= stats[0] -1;
pointSpend = pointSpend +1;
}
else if (staminaSpend){
stats[1]= stats[1] +1;
pointSpend = pointSpend -1;
}
else if (staminaReturn){
stats[1]= stats[1] -1;
pointSpend = pointSpend +1;
}
else if (constitutionSpend){
stats[2]= stats[2] +1;
pointSpend = pointSpend -1;
}
else if (constitutionReturn){
stats[2]= stats[2] -1;
pointSpend = pointSpend +1;
}
else if (agilitySpend){
stats[3]= stats[3] +1;
pointSpend = pointSpend -1;
}
else if (agilityReturn){
stats[3]= stats[3] -1;
pointSpend = pointSpend +1;
}
else if (intelligenceSpend){
stats[4]= stats[4] +1;
pointSpend = pointSpend -1;
}
else if (intellegenceReturn){
stats[4]= stats[4] -1;
pointSpend = pointSpend +1;
}
else if (willpowerSpend){
stats[5]= stats[5] +1;
pointSpend = pointSpend -1;
}
else if (willpowerReturn){
stats[5]= stats[5] -1;
pointSpend = pointSpend +1;
}
}
}
Answer by robertbu · Feb 07, 2014 at 12:26 AM
You can use GUITexture.HitTest(). This function takes a screen position. So for example if you wanted to test if 'staminaReturn' was under the mouse, you could do:
if (staminaReturn.HitTest(Input.mousePosition) {
Debug.Log("I'm over 'Stamina Return'");
}
@Robertbu thank you for such a quick response. It worked but i found out i offset my GUI Textures so they were not showing up in their original location. :/ So now that I am reorganizing them this is working perfectly.
Thank you so very much
@Robertbu thank you so so so so so so so much.... working perfect
Your answer
Follow this Question
Related Questions
Any way to show a picture on the screen when a model is clicked? 2 Answers
Passing through a GameObject/Function to a button's OnClick 1 Answer
Press button on canvas to transform prefab clone? 0 Answers
GUI button to Unity5 Button OnClic function 1 Answer
Button OnClick() after Scene restart is Missing (Object) 2 Answers