- Home /
how do u load a scene when pressing e on a object in game?
hello guys i have a small problem here that i thought u guys might know an answer too ;D, i have a problem with unity, i want to be able to load a scene when i press e on a object. i already did this on a button in the main menu using this code: public void LoadSceneMode(string name){ SceneManager.LoadScene(name); } public void QuitGame(){ Application.Quit ();
but that was a button and not a object in my game i have tried to change the code using Input.GetKeyDown(keyCode.E) but it just wont work does anybody of u guys know how to make this work? again i am not trying to change anything in the game just trying to open another scene
i am not a very experienced coder just doing it for fun ;D
Answer by aditya · Mar 28, 2016 at 12:09 PM
try this using unityEngine; using unityEngine.SceneManagement; using System.Collections;
public class loadScene : Monobehaviour{
void Update(){
if(Input.GetKeyDown(KeyCode.E))
SceneManager.loadScene(`SCENE NAME`);
}
}
Answer by Aelrift · Jun 10, 2017 at 10:41 AM
First of all , does your object have a rigidbody and a collider with trigger enabled? That might be why it doesn't work.You also used Input.GetKeyDown, which returns true fonly for the one frame you start pressing it. You should use Input.GetKey instead I have done the same thing in my work, here's is how I did it : public void OnCollisionEnter2D(Collision2D collision)
if (Input.GetKey(KeyCode.E) == true)
{
SceneManager.LoadScene("Realms of Eyre", LoadSceneMode.Single);
}
Where you would replace Realms of Eyre by the scene you want to load. Be sure to include using UnityEngine.SceneManagement at the very beginning of your code
Answer by FunIsDangerous · Jun 12, 2017 at 08:59 AM
Go to build settings, and press "Add Open Scenes" so that you see all your scenes there. The previous answers should explain the rest. Also, if by "when pressing e on a object" you mean looking on the object and pressing E, then you need to fire a Raycast towards where you are looking until it finds an object. If it does, and the object is the one you want (for example, a button), change the scene. You can also either get the object's position and calculate how far it is, or just limit how far the raycast will go
Your answer
![](https://koobas.hobune.stream/wayback/20220612073423im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Predfined Level Generation Procedural vs Scene 1 Answer
Help with loading scenes 2 Answers
Using parameters of activeSceneChanged? 0 Answers
Scene freezes after loaded 1 Answer
Scene loader Toutorial 0 Answers