Get your buttons working

void OnMouseDown ()
{
GameObject startButton = GameObject.Find (“Start”);
GameObject optionsButton = GameObject.Find (“Options”);
GameObject instructionsButton = GameObject.Find (“Instructions”);

	Application.LoadLevel ("_Scene_1");
}

so currently, i have 3 buttons in my scene, which are children of an empty object. I know how to get one button work each by a time, but by that, I would need to use 3 scripts. Is there like a way to script to let it be like: if start button is pressed, go to scene1.

void OnGUI() { if(GUI.Button(new Rect(50, 50, 50, 50),"Start")) { Application.LoadLevel("_Scene_1") } }

Application.LoadLevel("_Scene_1"); add the symecolon as well pls. Forgot it...

1 Answer

1

Buttons are often done by GUI Buttons as oppose to game objects. What’s more, it’s able to be aligned to the screen’s aspect ratio!

//Private Vars
private var MenuOn = false;
private var GridValue : float = -1;

//GUI Position and Size
var Window_Position : Vector2 = new Vector2(0, 0);
var Window_Size : Vector2 = new Vector2(360, 360);
var DragWindowPos : Rect = Rect(0, 0, Window_Size.x, Window_Size.y);

//Textures
var Menu_Window : Texture;

function OnGUI()
{
	GUILayout.BeginArea (new Rect (0, 0, 1000, 1000));
	GUILayout.BeginHorizontal();
	GUILayout.BeginVertical();
	
	if (MenuOn == true)
	{
		DragWindowPos = GUI.Window(0, DragWindowPos, MakeMyWindow, "");
	}
	
	GUILayout.EndVertical();
	GUILayout.EndHorizontal();
	GUILayout.EndArea();
}

function MakeMyWindow(WindowID : int)
{
	//Menu
	if (MenuOn == true)
	{
		GUI.BeginGroup(new Rect(Window_Position.x, Window_Position.y, Window_Size.x, Window_Size.y), Menu_Window);
			if (GUI.Button (new Rect (10, 160, 200, 40), "Restart" ))
			{
				Application.LoadLevel(1);
			}
			if (GUI.Button (new Rect (10, 205, 200, 40), "Resume" ))
			{
				MenuOn = false;
			}
			if (GUI.Button (new Rect (10, 250, 200, 40), "Shutdown" ))
			{
				Application.Quit();
			}
			//Draggable Window
			GUI.DragWindow (Rect (Window_Position.x, Window_Position.y, 10000, 40));
		GUI.EndGroup();
	}
}

Please tell me if you want to know how to auto-align the GUI. However, this is the coding for the GUI itself. I attach it to my character.