x


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;
                    }
                }
            }
        }
    }
more ▼

asked Apr 28 '11 at 07:40 PM

DibonaJ gravatar image

DibonaJ
21 4 4 10

Note: This is being called in the onGUI function

Apr 28 '11 at 07:43 PM DibonaJ
(comments are locked)
10|3000 characters needed characters left

1 answer: sort oldest

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

more ▼

answered Apr 28 '11 at 08:10 PM

DaveA gravatar image

DaveA
32.4k 158 176 278

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.

Apr 28 '11 at 08:16 PM DibonaJ
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5092
x288
x184

asked: Apr 28 '11 at 07:40 PM

Seen: 1291 times

Last Updated: Apr 28 '11 at 08:00 PM