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 AgentFoxtrot · May 04, 2015 at 01:39 PM · guiprefabbuttonloadlevel

Loading scene with a prefab GUI button?

I have looked everywhere on the internet and can't seem to find an answer for this. Maybe my solution is so simple that no one has ever bothered to ask the question.

I am using a prefab UI asset package I bought from the Asset Store (Holo UI, if it helps to know). So far I'm having no problems using it, other than that I cannot seem to figure out how to load a new scene when I click one of these prefab buttons.

I've emailed the asset creator for help, and he seemed confused that I was asking, pointing me to this tutorial on UI Events and Event Triggers. Fine. But this video and all the others I looked at never talk about prefab buttons, only using scripting to draw buttons:

 private void OnGUI( ) {
     if (GUI.Button(new Rect(x, y, w, h), "Load Level")) {
         Application.LoadLevel ("Level");
     }
 }

I get it, but I don't want to draw a new Rect. I want to use the prefab button I dropped on my canvas in Unity. I also tried messing with Event Triggers as the video above suggested, but I can't seem to get an OnClick ( ) -> LoadLevel( ) link going.

Can someone please help with step-by-step directions from adding the prefab GUI button to loading the scene? I'm at my wit's end here. Thank you very much.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by $$anonymous$$ · May 04, 2015 at 11:26 PM

  1. Add your button

  2. Create a new C# script and call it "ButtonHandler" and paste this code inside

    using UnityEngine; using System.Collections;

    public class ButtonHandler : MonoBehaviour { public void LoadScene(string sceneName) { Application.LoadLevel(sceneName); } }

  3. Click on your button in the heirarchy and add the ButtonHandler script to your button

  4. In the inspector find this part:

alt text

  1. Click the plus icon under On Click() (the red box in the photo above)

  2. Drag the button object from the heirarchy into the box that says "None (Object)" (red arrow below)

alt text

  1. Click on the "No Function" dropdown and under "ButtonHandler" click on "LoadScene(string)" (red box above)

  2. In the box below the drop down enter the name of your scene, for instance "Scene2"

  3. Finally go to File > Build Settings and in the "Scenes in Build" section add both the scene with the button and the scene you want to change to. You can do this by opening each scene and clicking "Add Current" under the list of scenes.

I hope this helped. Feel free to ask any further questions if you need clarification


buttonhelp.png (79.1 kB)
buttonhelp2.png (160.4 kB)
Comment
Add comment · Show 2 · 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
avatar image AgentFoxtrot · May 06, 2015 at 02:09 AM 0
Share

Bingo! Finally! Thank you very much!

avatar image $$anonymous$$ · May 07, 2015 at 01:50 PM 0
Share

No problem :)

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

2 People are following this question.

avatar image avatar image

Related Questions

On Click paramaters disappear from button prefab? 5 Answers

Change to scene based on current scene 1 Answer

Play sound on button click before loading level. 3 Answers

GUI button inconsistent -1 Answers

GUI Creates a prefab? 1 Answer


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