- Home /
Click Event isn't working when using GUI Button in random position
Hi, i' m new here so, excuse my ignorance ( and my English too ).
I' m trying to make a randomly positioned GUI.Button, it appears, but the the click event isn't working:
public void GetXandYs ()
{
x = UnityEngine.Random.Range (0, Screen.width * 0.9f);
y = UnityEngine.Random.Range (0, Screen.height * 0.9f);
delay ();
}
void delay()
{
DateTime a = DateTime.Now;
DateTime b = DateTime.Now.AddSeconds(1f);
while (a < b)
{
a = DateTime.Now;
}
}
private void OnGUI()
{
GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), background);
GUI.skin = myskin;
GUI.backgroundColor = Color.clear;
GetXandYs ();
if (GUI.Button (new Rect (x, y, Screen.width * 0.1f, Screen.height * 0.1f), image1)) {
Debug.Log ("Hello");
}else{
Debug.Log ("Hello2");
}
}
All i get is 'Hello2' even if i'm clicking the button constantly. I noticed that when i get the x and y axis stable, the click event works fine. Why is that, and how can i solve it ?
Also, every time the button appears i get two 'Hello2', and if i click on it ins$$anonymous$$d of 'Hello' i get more 'Hello2' and the button stays visible more time than the usual
Your answer
![](https://koobas.hobune.stream/wayback/20220612034402im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
move a button On click - rather than on game start 1 Answer
Pass Clicks Through Camera 2 Answers
Mouse click on a box 0 Answers
Click Object under another Object 1 Answer
click on a game object that script isn't attached to 1 Answer