Search Unity

How to change scenes by pressing a key

Discussion in 'Scripting' started by mileschaulk12, Oct 30, 2014.

  1. mileschaulk12

    mileschaulk12

    Joined:
    Oct 30, 2014
    Posts:
    12
    I have read multiple forms and nothing seems to be able to work. I just want to hit a key, an it load a different scene. Please help!
     
  2. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Simple.
    Code (CSharp):
    1. //Load a scene by the name "SceneName" if you press the W key.
    2. if(Input.GetKeyDown(KeyCode.W)){
    3. Application.LoadLevel("SceneName");
    4. }
     
    StaleyMemes and djfunkey like this.
  3. mileschaulk12

    mileschaulk12

    Joined:
    Oct 30, 2014
    Posts:
    12
    upload_2014-10-31_11-53-20.png
    Still not working "parsing error 2,2"?
     
  4. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    You need to put that in the Update function.

    If you use C#, your script should look like this :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SomeScript: MonoBehaviour {
    5.  
    6. void Update(){
    7. if(Input.GetKeyDown(KeyCode.Alpha1)){
    8. Application.LoadLevel("som3");
    9. }
    10. }
    11. }
    Replace "SomeScript" with the name of your script.

    If you use Javascript, your code should look like this :

    Code (JavaScript):
    1. function Update(){
    2. if(Input.GetKeyDown(KeyCode.Alpha1)){
    3. Application.LoadLevel("som3");
    4. }
     
    mileschaulk12 likes this.
  5. mileschaulk12

    mileschaulk12

    Joined:
    Oct 30, 2014
    Posts:
    12
    Thanks so much!
     
  6. LIONGATOR

    LIONGATOR

    Joined:
    Apr 9, 2017
    Posts:
    1
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    gpapalois and flav3 like this.
  8. bunnybean

    bunnybean

    Joined:
    Apr 26, 2017
    Posts:
    2
    But what if you also wanted the player to not be able to move to the end of the game until they've collected all the coins or something? is there a way to add a second thing to your if function?
     
  9. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Yeah, you can do this by creating a new bool:
    Code (CSharp):
    1. public bool collectedAllCoins;
    Then check if bool this is true in the if statement:
    Code (CSharp):
    1. if(Input.GetKeyDown(KeyCode.Alpha1) && collectedAllCoins)
    2. {
    3.     //Load the scene
    4. }
     
  10. bunnybean

    bunnybean

    Joined:
    Apr 26, 2017
    Posts:
    2
    Thank you so much! It works perfectly thanks to you.
     
  11. VertilogyMC

    VertilogyMC

    Joined:
    Dec 25, 2017
    Posts:
    2
    What if the keypress changed scenes on delay by about 4 seconds? Anybody have code for this?
     
  12. ThomasCoder223

    ThomasCoder223

    Joined:
    Apr 30, 2020
    Posts:
    1
    When I save the script Unity gives me an error saying that:
    Assets\Scripts\LevelController.cs(8,29): error CS0117: 'KeyCode' does not contain a definition for 'Car'
    But I have an input key called "Car" in the Input Manager.
    What's the problem?
     
  13. Please show us what are you doing.

    I assume(!) because you haven't shown anything, that you are trying to use something like this:
    Code (CSharp):
    1. [...]
    2. if (Input.GetKeyDown(KeyCode.Car))
    3. [...]
    This uses actual keys like KeyCode.Space and so on... what you're looking for (again if my assumption is true) is something like:
    Code (CSharp):
    1. [...]
    2. if (Input.GetButtonDown("Car"))
    3. [...]
    https://docs.unity3d.com/ScriptReference/Input.GetButtonDown.html
     
  14. WizardPasindu

    WizardPasindu

    Joined:
    May 13, 2020
    Posts:
    1
    I wrote a script when I Press Escape Button Unity Go To The Main Menu.The Script is Working Unity Have Loaded The MainMenu Scene But I Can't Press My Buttons In MainMenu When I Came From The Previous Scene

    using System.Collection;
    using System.Collection.Generetic;
    using UnityEngine;
    using UnityEngine.SceneManagement;

    public class pausemenu3 : MonoBehaviour
    {
    void Update(){
    if(Input.GetKeyDown(KeyCode.Escape))
    {
    SceneManager.LoadScene(1);
    }
    }
    }
     
  15. 1; open your own thread please, it is confusing having multiple question in one
    2; use code tags to post your script it is unreadable for humans: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
  16. grovedupreez61

    grovedupreez61

    Joined:
    Apr 26, 2021
    Posts:
    1
    Do I need to add the script to anything?
     
  17. Realspawn1

    Realspawn1

    Joined:
    Jun 8, 2015
    Posts:
    128
    There is an example of what you need in my magazine. its free to use. It tells you what it does and how you can use it.

    rp-interactive.nl

     
  18. eldiplo

    eldiplo

    Joined:
    Sep 17, 2022
    Posts:
    1
    Anyone can help me?
    From what I was seeing, a void update is needed for the use of the keys. I have the following void to change scenes with a box collider. Is there any way to be inside the box collider and at the same time press a key to change the scene?
    Code (CSharp):
    1.     [SerializeField] private string newLevel;
    2.  
    3.     private void OnTriggerEnter2D(Collider2D other)
    4.     {
    5.         {
    6.             if (other.CompareTag("Player") )
    7.             {
    8.                 SceneManager.LoadScene(newLevel);
    9.             }
    10.         }
    11.  
    12.     }
     
  19. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,192
    OnTriggerEnter2D only fires a single time so you would have to be pressing the key during that exact instant. What you want is OnTriggerStay2D. This fires every frame that the collision is occurring.

    https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerStay2D.html
     
  20. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    BUT... you still CANNOT reliably ask for key down events in OnTriggerStay2D either. It might work but it is also specifically disclaimed to be reliable, and may fail at any time, especially on faster computers.

    You need to observe that you have entered or exited the trigger and set / clear a boolean, perhaps
    bool IsNearExit;


    Then in Update(), the ONLY place you can safely check key downs, check the bool, and if it is true, check the key and go.

    There are literally thousands of tutorials for "Press E to open a door" or "Press E to exit level" or "Press E to open a chest." These are all the same principles... go learn from them.

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!

    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    Finally, when you have errors...

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.
     
    Serosin and Ryiah like this.