- Home /
How do I make a working button?
I just started using Unity the other day and, as such, I'm a total noob. While I've been able to learn how to set up a scene and navigate it with a working player controller, I can't figure out how to make a working main menu from which you can chose which level to go to by clicking a button. I don't know java, which has proved to be a problem, and every time I browse the forums to get sample code I get the following: "All compiler errors have to be fixed before you can enter playmode!" with the error: "UCE0001 ";" expected. Insert a semicolon at the end." Sorry for the ignorance. Any ideas?
Here this is the Script Reference to your question : http://docs.unity3d.com/Documentation/ScriptReference/GUI.Button.html
Alright, so here's my script;
pragma strict
/ Draws 2 buttons, one with an image, and other with a text // And print a message when they got clicked. var btnTexture : Texture; function OnGUI() { if (!btnTexture) { Debug.LogError("Please assign a texture on the inspector"); return; } if (GUI.Button(Rect(10,10,50,50),btnTexture)) Debug.Log("Clicked the button with an image"); if (GUI.Button(Rect(10,70,50,30),"Click")) Debug.Log("Clicked the button with text"); } function Start () {
}
function Update () {
}
A print-screen of my workspace is attached.
The button is now clickable and the console gives me verification that I'm clicking it correctly, however how do I take this make it send the player to the next scene?
Application.LoadLevel will load the scene for you. http://docs.unity3d.com/Documentation/ScriptReference/Application.LoadLevel.html
Please put your code in the code formatting so that we can help you better.
I can't thank you guys enough. I've been able to piece together how to code in Java because of the responses and I've been able to make a working button. $$anonymous$$y only other problem lies in that I can't figure out how to choose the location on the screen of where I want the buttons to go. When I try to code 2 buttons, only one of them is selectable because the other button is put in the exact same location. Here's the code (in proper format this time, haha)
#pragma strict
// Draws 2 buttons, one with an image, and other with a text
// And print a message when they got clicked.
var btnTexture : Texture;
function OnGUI() {
if (!btnTexture) {
Debug.LogError("Please assign a texture on the inspector");
return;
}
if (GUI.Button(Rect(10,10,50,50),btnTexture))
Application.LoadLevel ("level better");
}
function Start () {
}
function Update () {
}
Answer by eightbitstev · Mar 18, 2014 at 04:50 PM
What might be easier for you (instead of working with the GUI class) is to put something in the scene, like a plane, to represent your button. Stick a BoxCollider on it or a BoxCollider2D to use as your button's clickable area. If you attach a script to the same GameObject that has this collider, you can use the OnMouseDown() callback to do stuff in code when your button is clicked.
Your answer
Follow this Question
Related Questions
I have a main menu, and I want to have another button with more games. How can I do that? 0 Answers
problem with button touch zone 0 Answers
UI buttons are clickable but not performing task after go back to main menu 1 Answer
jump button in 3d android game 0 Answers
How to make in main menu? 5 Answers