- Home /
Multiple functions in a single button not working
I'm attempting to make a build menu, RTS style, in which several buttons with graphics appear when you right-click, then you click one of those buttons, it prints the button's name, then turns the build menu off.
So far I have gotten the build menu to turn on with right click, show the buttons in their proper places, assign textures to those buttons, have the build menu turn off when I right click, make the window dragable... but when I click one of the buttons nothing happens.
What exactly am I doing wrong here? The button code worked just fine until I put buildMenuOn = false; in there.
(Yes, this is designed to have 2 rows of 4 buttons each. I haven't yet made textures or assigned the other buttons yet, and only have 3 so far.)
#pragma strict
var buildMenuOn : boolean;
var buildRect : Rect = Rect (20,20,235,300);
// Textures for the buttons
var buttLivingQuarters : Texture;
var buttCafeteria : Texture;
var buttMedical : Texture;
function Start ()
{
var buildMenuOn = false; // the build menu starts out off
}
function Update ()
{
if (Input.GetButtonDown("Fire2")) // if right mouse button is clicked
{
if(buildMenuOn) // if the build menu is visible
{
buildMenuOn = false; // turn it off
}
else // if the build menu is not on
{
OnGUI(); // bring up the build menu GUI
buildMenuOn = true; // turn the build menu on
}
}
}
function OnGUI()
{
if(buildMenuOn)
{
buildRect = GUI.Window(0,buildRect,BuildWindow,"Please select your Prefab");
}
}
function BuildWindow(windowID: int)
{
// Draggable Windows
GUI.DragWindow(Rect(0,0,10000,10000));
//build button controls
// top line
if (GUI.Button(Rect(10,20,50,50),buttLivingQuarters))
{
print("Living Quarters");
buildMenuOn = false;
}
if (GUI.Button(Rect(65,20,50,50),buttMedical))
{
print("Medical");
buildMenuOn = false;
}
if (GUI.Button(Rect(120,20,50,50),buttLivingQuarters))
{
print("Living Quarters");
buildMenuOn = false;
}
if (GUI.Button(Rect(175,20,50,50),buttLivingQuarters))
{
print("Living Quarters");
buildMenuOn = false;
}
//second line
if (GUI.Button(Rect(10,75,50,50),buttCafeteria))
{
print("Living Quarters");
buildMenuOn = false;
}
if (GUI.Button(Rect(65,75,50,50),buttCafeteria))
{
print("Living Quarters");
buildMenuOn = false;
}
if (GUI.Button(Rect(120,75,50,50),buttCafeteria))
{
print("Living Quarters");
buildMenuOn = false;
}
if (GUI.Button(Rect(175,75,50,50),buttCafeteria))
{
print("Living Quarters");
buildMenuOn = false;
}
}
Answer by ArkaneX · Sep 25, 2013 at 07:00 PM
Please move this line
GUI.DragWindow(Rect(0,0,10000,10000));
to the end of OnGUI method. This should fix it.
Additionally, you don't have to explicitly call OnGUI (and besides, currently calling it does nothing, as it is called when buildMenuOn is still false). So I suggest changing the code of your right mouse button test to:
if (Input.GetButtonDown("Fire2")) // if right mouse button is clicked
{
buildMenuOn = !buildMenuOn;
}
Thanks for the quick answer!
It was really that simple of an answer? Well, don't I feel silly. I actually stared at that for a while wondering.
As for the !build$$anonymous$$enuOn, what exactly does that do? I haven't seen any tutorials or script using the ! yet. Is that for Java or C#?
Converted your answer to a comment (please post only real answer as answers).
! sign is unary logical negation operator (equivalent of NOT). It returns true if its operand is false, or false if its operand is true. Works in both C# and JavaScript.
As a side note - if an answer helped, please accept it using an icon at its top, under upvote/downvote icons. This way users here gain karma (or you can say experience, like in RPG) :)
New to the way this is done. Have to have 15 rep to use the arrow first. Thanks for your help though, you've been seriously helpful.
15 karma is required to upvote, but all users can accept answers to their own questions. It's the third icon.
Your answer
Follow this Question
Related Questions
Button doesn't work after build 1 Answer
Distribute terrain in zones 3 Answers
Button Animation Bug 1 Answer
How do you make a Main Menu that only takes Joystick input and is not affected by the Cursor 2 Answers
How to make Iphone Menu-Unity 2 Answers