Buttons help

Hello, i am a bit puzzled. I trying to get each button work separtly on the toolbar. Can someone help me with it. I got this up to now.

/* GUI.Toolbar example */

var newSkin : GUISkin;
var toolbarInt = 0;
var toolbarStrings : String[ ] = [“Lobby”, “Play”, “Shop”, “LeaderBoard”, “Chat”];

function OnGUI ()
{
//load GUI skin
GUI.skin = newSkin;
toolbarInt = GUI.Toolbar (Rect (5, 42, 480, 45), toolbarInt, toolbarStrings);
}

ok, got one working. Now how do i add the next one because it wont let me do function Update() again, Please Help.

/* GUI.Toolbar example */

var newSkin : GUISkin;
var toolbarInt = 0;
var toolbarStrings : String[ ] = [“Lobby”, “Play”, “Shop”, “LeaderBoard”, “Chat”];

function OnGUI ()
{
//load GUI skin
GUI.skin = newSkin;
toolbarInt = GUI.Toolbar (Rect (5, 42, 505, 45), toolbarInt, toolbarStrings);
}

function Update ()
{
// If clicked go to lobby
if(Input.GetButtonDown(“Mouse X”))
toolbarStrings = [“Lobby”];
Debug.Log(“You selected Lobby”);
}

You code, “if(Input.GetButtonDown(“Mouse X”))”, has nothing to do with your toolbar; that’s actually asking if the mouse has moved horizontally, which is obviously not what you want. You’re also resetting the list of buttons in the toolbar to just one item, which will make all the other buttons disappear. Probably also not what you want.

Here’s a more typical usage (omitting the code that’s irrelevant to the question):

var toolbarInt = 0;
var toolbarStrings : String[] = ["Lobby", "Play", "Shop", "LeaderBoard", "Chat"];

function OnGUI () 
{
    toolbarInt = GUI.Toolbar (Rect (5, 42, 505, 45), toolbarInt, toolbarStrings);
}

function Update ()
{
    // If clicked go to lobby
    if (toobarInt == 0) 
    {
        Debug.Log("You selected Lobby");
    } 
    else if (toolbarInt == 1)
    {
        Debug.Log("You selected Play");
    }
    else if ...
}

Does that work better for you?

nope that does not work. I made a new one and for some reason it will not let me go back to the lobby page.

/* GUI.Toolbar example */

var newSkin : GUISkin;

function OnGUI ()
{
//load GUI skin
GUI.skin = newSkin;
GUI.Button (Rect (5, 43, 90, 40), “Lobby”);
if (Input.GetMouseButtonDown(0))
Application.LoadLevel (“Main Menu”);

//load GUI skin
GUI.skin = newSkin;
GUI.Button (Rect (100, 43, 90, 40), “Play”);
if (Input.GetMouseButtonDown(0))
Application.LoadLevel (1);

//load GUI skin
GUI.skin = newSkin;
GUI.Button (Rect (195, 43, 90, 40), “shop”);
if (Input.GetMouseButtonDown(0))
Application.LoadLevel (2);

//load GUI skin
GUI.skin = newSkin;
GUI.Button (Rect (290, 43, 100, 40), “Leaderboard”);
if (Input.GetMouseButtonDown(0))
Application.LoadLevel (3);

//load GUI skin
GUI.skin = newSkin;
GUI.Button (Rect (395, 43, 90, 40), “Chat”);
if (Input.GetMouseButtonDown(0))
Application.LoadLevel (4);
}

It should work (excepting any syntax errors); it just may not do what you want – you haven’t really described what you’re trying to achieve.

You’re still confusing GUI input/interaction with general mouse input. What that code is doing:

  • Draw a GUI button, but ignore whether it gets clicked or not
  • See if mouse button one was pressed down this frame, regardless of where the mouse may be
  • If the mouse was clicked, anywhere, call LoadLevel(“Main Menu”)

You then continue in the same vein – so, what this code will do is: draw 5 buttons but ignore them; check to see if the mouse was clicked and, if so, load all five levels (since if the mouse button is pressed, Input.GetMouseButtonDown(0) will return true every time you call it).

You really need to re-read the GUI Scripting Guide and make sure you’re following the structure of the code in the examples. In this case what you wanted would look more like:

function OnGUI () 
{
	if (GUI.Button (Rect (5, 43, 90, 40), "Lobby")) 
	{
		Application.LoadLevel ("Main Menu");
	}
	...
}

You need to test if the button was clicked, not just if the mouse was clicked.