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
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
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