I can't find working solution for this problem anywhere. Here is what I'm trying to do:

My Android game contains 10+ levels, user quits game on level 2. Next time user starts game it load level 2 automatically. Thats all I want, no need to save objects, just load last scene. I prefer simple javascript, but examples on the unity tutorials confuse me. Thanks for answers.


When player quit from game you should save current level number so when player will come again in game should load last current level.

when player finish current level save next level number so when player quit and run game again loaded next level.

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class LevelManager : MonoBehaviour {
    public enum Situation{None,Exit,Lose,Win
    public Situation mycase;

    // Use this for initialization
    void Start () {
        Application.LoadLevel (PlayerPrefs.GetInt("Level"));

    // Update is called once per frame
    void Update () {
        int LevelNumber = SceneManager.GetActiveScene ().buildIndex;
        case Situation.Exit:
            PlayerPrefs.SetInt ("Level", LevelNumber); //Save Level number
            Application.Quit ();
            mycase = Situtation.None;
        case Situation.Lose:
            Application.LoadLevel(Application.loadedLevel);//Reset Level
            mycase = Situtation.None;
        case Situation.Win:
            PlayerPrefs.SetInt ("Level", LevelNumber + 1); //Save Level next number
            Application.LoadLevel (LevelNumber + 1); //Load NextLevel
            mycase = Situtation.None;
  • \$\begingroup\$ I have no idea how to use it or it doesn't work. I add this to an empty object and level 1 loads every 5 seconds. \$\endgroup\$ – Matt Jul 31 '16 at 18:41
  • \$\begingroup\$ What those tick boxes are for? \$\endgroup\$ – Matt Jul 31 '16 at 18:50
  • \$\begingroup\$ because you should add scenes from File > BuildSetting >>Add Open scene.I use tick boxes for check condition this is mean when condition is true for example if Lose is true Reset Level \$\endgroup\$ – Seyed Morteza Kamali Jul 31 '16 at 18:51
  • \$\begingroup\$ All the scenes are add. I appreciate your work man, and would like to get this working. Because now it just glitches out. Should I add this to all scenes or one is enough? \$\endgroup\$ – Matt Jul 31 '16 at 18:53
  • 2
    \$\begingroup\$ Using boolean flags for chain if else checking is not a good practice. Use an Enum instead. \$\endgroup\$ – S. Tarık Çetin Jul 31 '16 at 19:02

In your case you need just one interger value be saved and loaded.

In your scene load method, use these lines. Put your first game scene index instead of 2.

if (PlayerPrefs.GetInt ("current_level") == null)
PlayerPrefs.SetInt ("current_level", 2);//assuming level 0 is splash screen, level 1 is main menu and level 2 is first scene. Change as your need
Application.LoadLevel(PlayerPrefs.GetInt ("current_level"));

On every scene use the below method in an active object.

void OnApplicationQuit()
PlayerPrefs.SetInt ("current_level", Application.loadedLevel);

PlayerPrefs is not suggested in large scale data handling. Use binaryformatter, xmlserializer etc for that purposes. You will get more control over data.

If you are not using Application.Quit () for game exit then use it. Or you can put the line in OnApplicationQuit () to Start () on an active object in all scene. That way even if you don't quit the game, just going into the scene will be saved.

Switch to JS to see how to write JS function which is written in C#

  • \$\begingroup\$ Scene 1 keeps reloading :/ \$\endgroup\$ – Matt Jul 31 '16 at 19:13
  • \$\begingroup\$ You used the second method in all scenes? \$\endgroup\$ – Sourav Paul Jul 31 '16 at 19:25
  • \$\begingroup\$ I put the first one method in first scene and the second method in every other scene. It get's stuck on scene 1. My all other scripts are in js. \$\endgroup\$ – Matt Jul 31 '16 at 19:45
  • \$\begingroup\$ It's almost same in js, I assume you know how to covert the small thing that's not same. Are you quitting the scene with Application.Quit (); ? \$\endgroup\$ – Sourav Paul Jul 31 '16 at 19:50
  • \$\begingroup\$ I'm not using Application.Quit (); and i don't know how to convert this code to js. \$\endgroup\$ – Matt Aug 1 '16 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.