- Home /
How do I show a gui.button after clicking on a specific object or disable gui.button until after click?
I'm having trouble implementing an onMouseDown() for a GUI.button. I want the button to show up only after the user clicks on Block1. Currently, the button is being shown on all views that contain Block1 or have Block1 as active.
Is there a way to just disable the button until Block1 is clicked on and then have the button show on from then but not when I restart the scene/go back to the previous script where block1 is not clicked on yet (even though block1 is active in that scene)?
So far I have something like this:
public bool showButton = false;
void onMouseDown() {
if(Input.GetMouseButtonDown(0))
showButton = true;
}
void OnGUI () {
// Make a background box
GUI.Box(new Rect(10,10,250,200), "Menu");
if (GameObject.Find("block1") && showButton== true) {
if(GUI.Button (new Rect (30,40,200,70), "Back to the block with stripes ")) {
print ("You clicked the button!");
mainCam.transform.position= new Vector3(-.13f, 0.87f, -8);
Camera.main.orthographicSize = 0.4f;
}
The only possibility that I was thinking about logically is if I said something like GameObject.Find("Block1").onMouseDown()) for the if condition but I don't think Unity accepts those parameters? I think I'm mostly confused on how to word the logic so that I can make this work.
You could create a script that sends the message, when the block is clicked, also if needed use bools to enable UI. :)
Hi,
I was wondering, what is wrong with having the On$$anonymous$$ouseDown void on the same GameObject as you have the GUI void?
Answer by KaTsuoo1 · Jan 16, 2014 at 06:17 AM
You would need a way for the game to know that you have clicked the block, which you could use a boolean variable for. So at the top of the script where you would create your variables, you could put:
var Block1Clicked : boolean = false;
Then you would change the first if statement to:
if (GameObject.Find("Block1") && Block1Clicked == true) {
Then all you have to do is to enable that variable when you have clicked on Block1, with the following line:
Block1Clicked = true;
I guess my main question is how do I get on$$anonymous$$ouseDown to work within onGUI? Do I just call on$$anonymous$$ouseDown outside of onGUI? How do I get on$$anonymous$$ouseDown to work with gui.button?
I edited my code to show my alterations. Even with a boolean, it doesn't work the way I want it too. With the bool, the button is disabled all the time and does not activate on$$anonymous$$ouseDown. I'm not sure why...
It should be On$$anonymous$$ouseDown. Ins$$anonymous$$d of on$$anonymous$$ouseDown, also your are using the same game object to make the GUI and react to clicks.
What do you mean by same game object? Hmm. I fixed the typo for On$$anonymous$$ouseDown and it still disables it all the time...
What HuskyPanda213 meant was that you have the On$$anonymous$$ouseDown void on the same GameObject as you have the GUI void, since it is all in the same script. However, I doubt I will be able to help you further because I don't use C#, I just knew that booleans would work the same.
Your answer