Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to check my current scene and when press back button(in mobile) ,go to lower scene?

Discussion in 'Scripting' started by babji3, Sep 14, 2015.

  1. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    Hi all,
    Here i have two scenes , Scene-0(menu) and Scene-1(game scene).
    When am in game scene and press back button in mobile i have to go in Scene-0(menu),
    and again when i press back button the game should be Quit.

    Please give me some code lines to go with this.I know this is very small Question.
     
  2. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
  3. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    Hi, Actually i dont want any extra UI in my scene/canvas.
    I want just use back button of the mobile.
    Am using C#
    Thx.
     
  4. ritesh_khokhani

    ritesh_khokhani

    Joined:
    Jun 26, 2013
    Posts:
    47
    Hey babji3,

    Unity allows to check name of current scene using Application.loadedLevel which returns name of current scene,

    http://docs.unity3d.com/ScriptReference/Application-loadedLevel.html

    Here is the code for what you want,
    Code (CSharp):
    1.     void Update ()
    2.     {
    3.         if(Input.GetKeyDown(KeyCode.Escape))
    4.         {
    5.             if(Application.loadedLevelName == "Game")
    6.             {
    7.                 Application.LoadLevel("Menu");
    8.             }
    9.             else if(Application.loadedLevelName == "Menu")
    10.             {
    11.                 Application.Quit();
    12.             }
    13.         }
    14.     }
    Also don't forgot to write DontDestroyOnLoad(this.gameObject); in script the above code is written..

    http://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
     
  5. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    Thx khokhanu, I don't know we can check by name itself. I'll try this.