Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Shaneguy · Mar 13, 2014 at 03:34 PM · buttonmain menufirst person

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?

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GEWLAR · Mar 13, 2014 at 03:49 PM 0
Share

Here this is the Script Reference to your question : http://docs.unity3d.com/Documentation/ScriptReference/GUI.Button.html

avatar image Shaneguy · Mar 18, 2014 at 03:31 PM 0
Share

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. alt text

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?

workspace.png (471.0 kB)
avatar image karljj1 · Mar 18, 2014 at 04:24 PM 0
Share

Application.LoadLevel will load the scene for you. http://docs.unity3d.com/Documentation/ScriptReference/Application.LoadLevel.html

avatar image stevethorne · Mar 18, 2014 at 04:30 PM 1
Share

Please put your code in the code formatting so that we can help you better.

avatar image Shaneguy · Mar 18, 2014 at 04:52 PM 0
Share

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 () {
 
 }
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges