Jazib
Jazib

Reputation: 3

Back button on android device is not working

In unity2017 I'm trying to use android device's back button to go to scene 1. Here is my code:

if(Input.GetKey(KeyCode.Escape)){
    SceneManager.LoadScene(1);
}

This code is not working, I tested it unity editor as well as after building apk on device. Anyone have idea how to make it work?

Upvotes: 0

Views: 517

Answers (1)

Programmer
Programmer

Reputation: 125275

There is nothing wrong with if(Input.GetKey(KeyCode.Escape)) in Unity and it should execute as long as that code is placed in the Update function.

In order for the Update function to be called, the folliwing must be true:

1. The script it is inside is enabled

2. The GameObject that script is attached to is active and is not destroyed with Destroy function.

If the above are already true then possible problem is that you did not add the scene to the Build Settings.

You need to add the scenes to the Build Settings, otherwise SceneManager.LoadScene(1); won't work.

Note that SceneManager.LoadScene(1); means that it will load scene with index 1. This means that you must have two scenes for index 1 to be valid since index starts from 0 not 1. If you have one scene, use SceneManager.LoadScene(0); or load the scene by name SceneManager.LoadScene("Name of Scene"); but make sure they are added to the Build Settings.

Below is how to add scenes to the Build Settings.

enter image description here

Upvotes: 1

Related Questions