- Home /
How do I use a toolbar?
Hello. How do I use the toolbar GUI in unity? I know how to display the buttons, but how to I make them affect something? I have tried something like this: (and it does not work)
EDIT: I changed the Static var Mode=float to "static var Mode = 5;"(I used 5 because I am only using 0-4 in the other script, bit I guess that doesn't really matter, I also tried using the " : float" thing and I am getting no errors. Anyways, I still have into a somewhat hard to explain problem.
If I define something to happens in the script that has the toolbat GUI like this, the other variable (in this case, "Score") works. Its changed to whatever is defined.
if (ModeInt==2) {Stats.Score=1000}
I then try to make a variable in the GUI script affect a variable in the "ArcadeOptions" script:
if (ModeInt==0) {ArcadeOptions.Mode =2;}
Here is the ArcadeOptions Script
static var Mode = 5;
if (Mode ==0) {Stats.Score=1000;}
and it works, the Score value is changed.
The problem happens when I try to have the "ModeInt==" something other than 0(so that other buttons can affect the "ArcadeOptions" script). No matter what the element of that toolbar is, if it is not 0, nothing happens in the other script, what is going on(I am getting no errors from unity)?
Answer by Statement · Dec 11, 2010 at 11:12 PM
Seems you're testing the string array instead of the selected item number.
function Update()
{
if (ModeStrings == 2) // Error
ArcadeOptions.Mode = 2;
}
If you change that to this, I'm sure you'll be fine.
function Update()
{
if (ModeInt == 2)
ArcadeOptions.Mode = 2;
}
You have further errors:
static var Mode = float; // Error static var Mode : float; // Ok
// and...
function Update() { if (ModeInt = 2) ArcadeOptions.Mode = 2; // Bug if (ModeInt == 2) ArcadeOptions.Mode = 2; // OK }
See the manual on gui controls (quote) for further info:
The Toolbar Control is essentially a row of Buttons. Only one of the Buttons on the Toolbar can be active at a time, and it will remain active until a different Button is clicked. This behavior emulates the behavior of a typical Toolbar. You can define an arbitrary number of Buttons on the Toolbar.
Basic Usage
The active Button in the Toolbar is tracked through an integer. You must provide the integer as an argument in the function. To make the Toolbar interactive, you must assign the integer to the return value of the function. The number of elements in the content array that you provide will determine the number of Buttons that are shown in the Toolbar.
/* GUI.Toolbar example */
var toolbarInt = 0; var toolbarStrings : String[] = ["Toolbar1", "Toolbar2", "Toolbar3"];
function OnGUI () { toolbarInt = GUI.Toolbar (Rect (25, 25, 250, 30), toolbarInt, toolbarStrings); }
And further down:
To detect if the user did any action in the GUI (clicked a button, dragged a slider, etc), read the GUI.changed value from your script. This gets set to true when the user has done something, making it easy to validate the user input.
A common scenario would be for a Toolbar, where you want to change a specific value based on which Button in the Toolbar was clicked. You don't want to assign the value in every call to OnGUI(), only when one of the Buttons has been clicked.
Basically you must check if (GUI.changed) if any change was done immediately after your toolbox.
Thanks. I changed it to $$anonymous$$odeInt and I get this error Assets/Scripts/$$anonymous$$enu/Arcade.js(33,45): BCE0022: Cannot convert 'int' to 'System.Type'. Tyler 43 $$anonymous$$s ago
$$anonymous$$ode is a variable located in a seperate script called "ArcadeOptions". These are the scripts for the selecting the options for my game (mode, difficulty, etc.). How it works is that the menu is a bunch of toolbars. Each toolbar option selected represents a number, that number represents a number in a different script, which contains the functions that start the options (like "Standard difficulty=true"). I will add the scripts in my edited question.
Btw, another error. In your update function you have if ( $$anonymous$$odeInt = 2 ), it should use == not =. And please number your lines or something when you are posting an error message. Assets/Scripts/$$anonymous$$enu/Arcade.js(33,45) says the error is on line 33, column 45 in Arcade.js. I can't possibly know if your code is from that range.
static var $$anonymous$$ode = float; That's the error. static var $$anonymous$$ode : float; it should be.