- Home /
Dynamic created button at Mouse Location Moving
I'm trying to create a button when a user clicks on a location on my terrain, it will later have a number of little buttons as images which they can click one and terrain will change depending on the button. But in the process of testing this I've ran into a problem that might be easily solved but as yet I haven't figured it out. I have a method on a in Update() for Input.GetMouseButtonDown(0), this does some other things but in one of the if functions it sets bool variable youSure to true to trigger the OnGUI if function below. The idea was it would display a button as the mouse location for the moment just with yes. But problem is the button moves with the mouse pointer so I can't actually even click it.
Is this because the calling that sets youSure = true after a mouseButtonDown event hasn't finished the if (GUI.Button(new Rect(button.x, button.y, 100, 20), "Yes")){
means that it just keeps running this if statement over and over thus creating new buttons as you move the mouse or should it as I assume it would, once you create the button would stay in that x,y location and not move until clicked and closed?
void OnGUI(){
if(youSure == true){
button.x = Input.mousePosition.x;
button.y = Screen.height - Input.mousePosition.y;
if (GUI.Button(new Rect(button.x, button.y, 100, 20), "Yes")){
terrain.PaintTile1x1(tile, 2);
}
}
}
I'm pretty sure that you answer your own question. Try assigning the mousePosition.x/y to new variables when you release the mouse button and then assign those to button.x / y
I tried a few things to figure out whats going wrong, I even changed the code to something like this and least I get the button come up and stay up, but only down side is when you click the button to activate it's function in the if statement, it get's detected as another mouseclick and opens a new button. Is there anyway to have the button respond only to the click without changing the activation to be say button(2) so left clicking yes won't open a new one?
void Update(){ if(Input.Get$$anonymous$$ouseButtonDown(0)){ button.x = Input.mousePosition.x; button.y = Screen.height - Input.mousePosition.y; youSure = true; } void OnGUI(){ if(youSure){ if (GUI.Button(new Rect(button.x, button.y, 100, 20), "Yes")){ terrain.PaintTile1x1(tile, 2); youSure = false; } } }
Your answer
Follow this Question
Related Questions
Right-click button using GUI class? 3 Answers
Menu button needs to be double clicked. 4 Answers
Button being pressed but an other button gets the effect. 0 Answers
Disable a GUI while left mouse button is Down? 2 Answers
How to detect if mouse over a button 2 Answers