- Home /
GUI button vanishes when I release mouse button
Hi all. I'm having some GUI button issues. I have a piece of code right now for when you select some objects a test question comes up, telling you to select a specific object. After you select that object I want a GUI button to pop up that says either correct or incorrect. For the most part I have it, only when I release the mouse button the GUI button disappears. I have tried different combos of GetMouseButton, GetMouseButtonDown, and GetMouseButtonUp. But when I use GetMouseButtonDown in place of GetMouseButton the GUI button doesn't even show up at all. My code follows:
if(allObjectsClicked) { if(isClicked == false) { GUI.Box (new Rect (350,10,350,120), "Please click the learning cube."); { if (Input.GetMouseButton(1)) { if (Physics.Raycast (ray, out hit)) { if(hit.collider.gameObject.name == "learningCube2" || hit.collider.gameObject.name == "learningCylinder2") { isClicked = true; } } } } }
if(isClicked)
{
if (Input.GetMouseButton(1))
{
if (Physics.Raycast (ray, out hit))
{
if(hit.collider.gameObject.name == "learningCube2")
{
if(GUI.Button(new Rect (350, 10, 350, 120), "That was correct. You may now continue."))
{
navButtonEnabled = true;
Debug.Log("Why isn't the GUI button staying, jerk?");
}
}
else
{
GUI.Box(new Rect (350, 10, 350, 120), "Sorry, the correct answer was the cube. Click to advance.");
Debug.Log("Why isn't the GUI box staying part II");
navButtonEnabled = true;
}
}
}
}
}
Answer by DaveA · Apr 28, 2011 at 08:10 PM
if (Input.GetMouseButton(1)) is true only while the mouse button is held down. As soon as you release it, those buttons won't be 'called' to draw themselves. I think maybe if you just remove that from after the isClicked test it might behave for you
I had to get rid of the if(Input...) and the if(Physics...) otherwise whatever my mouse was hovering over was being selected. Getting rid of both of them caused everything to work perfectly though. Thank you so much.