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 Peanut97 · Nov 17, 2013 at 02:31 AM · sceneloadlevelkeypress

Can't get simple script to work?

Hello All,

I'm not TOO bad of a Unity noob, but for some weird reason, I cannot figure this one out... In my scene, I have a Player. On the player, I placed this script:

 function Update(){
 
     if(Input.GetKeyDown("escape")){
     Application.LoadLevel("MainMenu");
 }

Basically, I am trying to load the Main Menu ("MainMenu" scene) when I press the escape key... but for some weird reason, it's not working... I totally realize that it is just some tiny, stupid mistake but I can't seem to figure it out. If anybody could help me out, I would really appreciate it!! Thank you so much!

Comment
Add comment · Show 2
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 Seregon · Nov 17, 2013 at 02:38 AM 0
Share

Have you added $$anonymous$$ain$$anonymous$$enu to the scenes list in the build settings?

avatar image Peanut97 · Nov 17, 2013 at 02:41 AM 0
Share

Yes I did. I though that was the problem at first, but hen I checked it... Do you have any ideas?

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Fr0stbite · Nov 17, 2013 at 08:56 AM

Maybe:

 function Update(){
  
     if(Input.GetKeyDown("escape")){
     Application.LoadLevel("1");
 }
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
avatar image
1

Answer by clunk47 · Nov 17, 2013 at 02:36 AM

I'd use the keycode instead of a string.

 if(Input.GetKeyDown (KeyCode.Escape))
 {
     //DoYourThing
 }
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
avatar image
1

Answer by Tomer-Barkan · Nov 17, 2013 at 08:15 AM

  1. If you're going to use Input.GetKeyDown(), then use keycodes and not strings:

    if (Input.GetKeyDown(KeyCode.Escape)) { Application.LoadLevel("MainMenu"); }

  2. It's better to use Input.GetButton() and set the keys in the Input Manager. That makes it easier for you to change if you wish, and it also allows the players to use Unity's key settings to change the buttons as they please. Read how to define the buttons in the input manager here: http://docs.unity3d.com/Documentation/Components/class-InputManager.html. Then use Input.GetButton("NameYouGaveTheKey"), using the name you defined in the input manager.

But anyway this doesn't really solve yourproblem since your script should work as well... So try the following to debug:

First check whether the Update() is running at all, and add a Debug.Log right before the if statement right before the if statement. Second, add a debug inside the if statement.

That will help you identfy the problem. If the Update is not running, it might be that the game object or script are disabled, or somewhere else in the update code you are using return; before the if statement occurs.

If it does go into the if statement, there might be a problem with Application.LoadLevel(). Maybe the level name is incorrect.

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
avatar image
0

Answer by SrNull · Nov 17, 2013 at 09:06 AM

Have your tried this?:

        if(Input.GetKeyDown(KeyCode.Escape)){
              Application.LoadLevel("MainMenu");
        }

Also make sure your scene is inside the list of the scenes on Build settings window.

Comment
Add comment · Show 1 · 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 Peanut97 · Nov 17, 2013 at 08:13 PM 0
Share

Thank you all so much for your help!! I got it. Thank you!

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

Multiple Cars not working 1 Answer

Next Scene Script Help 1 Answer

Reset The Scene ? 2 Answers

How to Set a bool to Individual Scenes (C#) 1 Answer

Gui label text change 0 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