Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Gng357 · Nov 28, 2018 at 03:17 PM · androidscene-switchingback

Android device Back button not going back to last scene

In Unity, I am trying to go back to the last scene by clicking the Device Android back button and not back to the main screen.

I tried to save the state of the last scene but I always get that the last scene is 0 instead of the correct scene number.

Here is my c# code:

 public class SceneLoader : MonoBehaviour {
 
     private static int lastScene;
 
 
     private void Update()
     {
         if (Input.GetKey(KeyCode.Escape))
         {
 
             Debug.Log("Last scene: "+ GetLastScene());
 
             SceneManager.LoadScene(GetLastScene());               
             return;
         }
     }
 
     public void LoadNextScene(int numberOfScene)
     {
         StartCoroutine(LoadAfterSeconds(numberOfScene));    
     }
 
     private IEnumerator LoadAfterSeconds(int numberOfScene)
     {            
         yield return new WaitForSeconds(1f);
         SceneManager.LoadScene(numberOfScene);
         SetLastScene(numberOfScene);
         gameObject.SetActive(true);
     }
 
     public static void SetLastScene(int level)
     {
         lastScene = level;
     }
 
     public static int GetLastScene()
     {
         return lastScene;
     }
 
 }


If I don't call SetLastScene at all, then lastScene is always 0 and so when I press the Android device back button or "Escape" it goes to main screen (number 0). But when I use SetLastScreen it always get the current scene number instead of the previous scene.

Thank you.

Comment
Add comment
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

2 Replies

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

Answer by Gng357 · Nov 29, 2018 at 02:36 PM

I have tested the code @HenBOMB but the SceneManagement.LoadLevel doesn't seem to work for me.

I have changed my code and now it works:

 public class SceneLoader : MonoBehaviour
 {
     private static int lastScene;
     private int currentScene;
 
     private void Awake()
     {
         currentScene = SceneManager.GetActiveScene().buildIndex;
     }
 
     private void Update()
     {
         BackButtonPressed();
     }
 
     public void LoadNextScene(int numberOfSceneToLoad)
     {
         StartCoroutine(LoadScene(numberOfSceneToLoad));
     }
 
     private IEnumerator LoadScene(int numberOfScene)
     {
         currentScene = SceneManager.GetActiveScene().buildIndex;
         SetLastScene(currentScene);
 
         yield return new WaitForSeconds(0.5f);
         SceneManager.LoadScene(numberOfScene);
         
     }
 
 
     private void BackButtonPressed()
     {
         if (Input.GetKey(KeyCode.Escape))
         {
 
             Debug.Log("Current scene: " + currentScene);
             Debug.Log("Last Scene (scene to load): " + GetLastScene());
 
             SceneManager.LoadScene(GetLastScene());
 
             currentScene = GetLastScene();
             lastScene -= lastScene;
             Debug.Log("Now the Current scene is: " + currentScene);
             Debug.Log("And the scene to load is: " + lastScene);
         }
     }
 
 
     public static void SetLastScene(int currentSceneToLastScene)
     {
         lastScene = currentSceneToLastScene;
     }
 
     public static int GetLastScene()
     {
         return lastScene;
     }
 
     public void QuitGame()
     {
         Application.Quit();
     }
 
 }

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
0

Answer by HenBOMB · Nov 28, 2018 at 11:08 PM

I think its because you never assigned the value of your 'lastScene', so it sets it as default 0. the script was kind of condusing to interpret so heres what i wrote:

 int currentScene;
 
 void Update(){
     if(Input.GetKeyDown(KeyCode.Escape)){
         StartCoroutine(LoadPreviousScene(1));
     }
 }
 IEnumerator LoadNextScene(float waitingTime){
     yield return new WaitForSeconds(waitingTime);
     currentScene++;
     SceneManagement.LoadLevel(currentScene);
 }
 IEnumerator LoadPreviousScene(float waitingTime){
     yield return new WaitForSeconds(waitingTime);
     currentScene--;
     SceneManagement.LoadLevel(currentScene);
 }
 int GetCurrentScene(){
     return current;
 }
  int GetNextScene(){
     return current++;
 }
  int GetPreviousScene(){
     return current--;
 }


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

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

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

Using Android Back button to navigate backwards through a menu 1 Answer

Android back button not registering 1 Answer

[Solved]handle android hardware buttons 1 Answer

Stop back key android from quitting application WHILE the scene is loading? 1 Answer

Unity backbutton crashes while on first scene 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