GUI button mouse click

ok, I’ve been on Unity for about 2 months and so far I’ve been pretty good at figuring things out, but my brain is mush right now. I have a town where I want my students to click on items and have several buttons show up with text (such as questions). So far, so good. Now, I want them to click on a button and get an answer. That’s where I’m stuck. I went through the GUI stuff on the site and scanned through this forum. I can’t get anything to happen when I click on a button in the GUI. Even when I copy/paste code from the reference manual. Can anyone point me in the right direction? I suck at coding so it would take me days to dink around with enough to make something happen. I want my students to click on a button that has text (question) and have an answer appear.

Thanks

To make a GUI.Button actually do something, you have to include it in an if statement. For example:

function OnGUI() {
   if (GUI.Button(Rect(10,10,60,20),"My Button")) {
      Debug.Log("Button was clicked!");
   {
}

Check out: http://www.unity-tutorials.com/

besides the paid tuts, there are 2 free ones. The first coincidently is on GUI functions and such. The other more recent is on basic scripting.

B.

Hi Jacob,

Thanks for the reply. That’s what I thought, but it doesn’t work. It just moves the cursor to the center of the screen and nothing happens. I even tried:

function OnGUI () {
  if (GUI.Button (Rect (10,10,150,100), "I am a button")) {
    print ("You clicked the button!");
  }
}

and nothing but cursor moving to center of screen. Any other ideas?

Hi B,

Thanks for the link. Same problem, so I must have an issue somewhere that not button related? I click the button and mouse goes to middle of screen, button doesn’t change.

ok, maybe I’m nuts. If I click on the button with my middle mouse button, it works fine, otherwise no go.

Do you have some custom mouse configuration? That code, as is, works perfectly fine here on my end (MacBook Pro with either my touchpad or an external 3-button mouse). Also, I suspect something else is afoot as none of the code you’re citing would move the mouse anywhere, nor does Unity even offer the ability to set the mouse position.

Tip: when you post code make sure to wrap it in a code block ([ code ]…[ /code ], without the spaces) for better formatting. I’ve edited your post for reference. :slight_smile:

Tip: post GUI-related questions in the UnityGUI section as you’ll likely get faster/better response times, I’m going to move this thread there. :slight_smile:

Thanks Higgy. I am using the WOW camera control I found in a different post that allows the keyboard to control movement instead of the mouse. Maybe something in that script? Any idea on what I should look for?

I suppose there could be conflicting code but that can easily be tested by using your code in a brand new empty test scene with just the code above. What happens then?

The Wow controller is your issue. I had the same one. Remove that script and retest - it will work.

well crudapples. It was the WOW character controller. Guess that takes me back to square one on how to get the keyboard to control the character instead of the mouse. Thanks for the help!

I figured it out in case anyone else has the same problem. In the WOW character controler about line 48 is some code about Screen.lookcursor = true;. I commented that section out and everything still works the way I want and the buttons work normally as well. The code as I commented out is here.

    /*
   if(Input.GetMouseButton(1) || Input.GetMouseButton(0)) 
      Screen.lockCursor = true; 
   else 
   */
      Screen.lockCursor = false;

P.S. Thanks to everyone that gave input. I work for a K-12 school so am a team of one and active participation in these forums has been invaluable!