Making the Game Resume on the Last played Scene

Hi, my game has several levels and it’s similarly layed out to the game aa and ff. I was wondering how I could get it so that when the player progresses through the levels and if they exit the game, when they relaunch it they can continue on the level they were on. But there is one complication, I don’t want to start it on their last level as soon as they start, I want to display the main menu but as soon as they click the play button it will take them to the level they were last on.

1 Like

Do you want the player to be able to select a previous level to replay it? If so you will need to have something that lets them see the levels they’ve played & select it. If not, & they can only ever play the scene they are up to, & they must start that level at the beginning each time, then what I’m doing (& I’ve kept it simple as I’m still doing PoC so aren’t optimising or getting a proper coder to look at it yet) is when the player reaches the exit point I increment an int (levelToPlay), store it in PlayerPrefs & then load the scene mapped to that int. So I don’t get stuck on if statements so that if levelToPlay==1 load scene 3 (because the intro is 0, menus 1 etc) All I have to do is ensure that the first playable scene sets the Internet to the scene value e.g. If the first playable level is unity scene 3 I set the Internet to 3, at the exit it increments to 4 which matches to scene 4 which is playable level 2 etc. When you have the player press the play button you could access the level from the PlayerPrefs & load that scene.

As I said, it’s a quick & dirty way to do it & likely not the best way but it achieves what I want at this time.

1 Like

@tedthebug 's solution is great. Except when loading scenes I’d suggest using strings instead of ints. It can be slightly less error prone this way. And it’s flexible if you elect to add extra scenes at the beginning of your build.

You can also use a hybrid. Name the levels gameplay0, gameplay1 ect.

1 Like

Do you know what’s the error in this code?

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class PlayerController : MonoBehaviour {

public Button LeftBtn;
public Button RightBtn;
public GameObject Player;

// Use this for initialization
void Start () {

LeftBtn = LeftBtn.GetComponent ();
RightBtn = RightBtn.GetComponent ();

}

void Update()
{
if (Player.transform.position.y = -10)
{
Application.LoadLevel (“GameOver”);
}
}

public void LeftClick()
{
Player.transform.position = new Vector2((Player.transform.position.x - 3), Player.transform.position.y);
}

public void RightClick()
{
Player.transform.position = new Vector2((Player.transform.position.x + 3), Player.transform.position.y);
}
}

No the player won’t be able to select the level to play, they will just keep progressing through the levels. Yes the player will start at the beginning each time.

The console probably does. Line number and error code?

Assets/Scripts/PlayerController.cs(21,38): error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position’. Consider storing the value in a temporary variable

and also

Assets/Scripts/PlayerController.cs(21,17): error CS0029: Cannot implicitly convert type float' to bool’

Can you please help me with my game via messaging?

Mind editing your earlier post with code tags?

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class PlayerController : MonoBehaviour {

    public Button LeftBtn;
    public Button RightBtn;
    public GameObject Player;

    // Use this for initialization
    void Start () {

        LeftBtn = LeftBtn.GetComponent<Button> ();
        RightBtn = RightBtn.GetComponent<Button> ();
   
    }

    void Update()
    {
        if (Player.transform.position.y = -10)
        {
            Application.LoadLevel ("GameOver");
    }
    }

    public void LeftClick()
    {
        Player.transform.position = new Vector2((Player.transform.position.x - 3), Player.transform.position.y);
    }

    public void RightClick()
    {
        Player.transform.position = new Vector2((Player.transform.position.x + 3), Player.transform.position.y);
    }
}

I’ve changed the script it’s now this:

using UnityEngine;
using System.Collections;

public class EnemyCollision : MonoBehaviour {

    public GameObject Player;


    void Update();
    {
       
        if (Player.transform.position.y == -10)
        {
            Application.LoadLevel(Application.loadedLevel);
        }
    }

    void OnTriggerEnter (Collider other) {
       
        if(other.gameObject.tag == "Player")
        {
            Application.LoadLevel(Application.loadedLevel);
        }
       
    }
   
}

But still getting these errors:

Assets/Scripts/EnemyCollision.cs(10,9): error CS1519: Unexpected symbol `{’ in class, struct, or interface member declaration

Assets/Scripts/EnemyCollision.cs(12,50): error CS1519: Unexpected symbol `==’ in class, struct, or interface member declaration

Assets/Scripts/EnemyCollision.cs(14,46): error CS1519: Unexpected symbol `(’ in class, struct, or interface member declaration

Assets/Scripts/EnemyCollision.cs(14,70): error CS1519: Unexpected symbol `)’ in class, struct, or interface member declaration

Assets/Scripts/EnemyCollision.cs(16,9): error CS8025: Parsing error

Anyone know ??

Still need help…

Parse errors normally mean:
you have to many brackets
to few brackets
missing a semi colon

I always start by clicking to the left of the very first set of { to see which the system highlights as the last one (this identifies if you have to many)

using UnityEngine;
using System.Collections;
 
public class EnemyCollision : MonoBehaviour {
 
    public GameObject Player;
 
    void Update()    // extra ; deleted
    {
        if (Player.transform.position.y >= -10)  // changed to >=. == is always problematic with floats
        {
            Application.LoadLevel(Application.loadedLevel);
        }
    }
 
    void OnTriggerEnter (Collider other) {
      
        if(other.gameObject.tag == "Player")
        {
            Application.LoadLevel(Application.loadedLevel);
        }
      
    }
  
}

You are going to have to learn to do these things yourself…

Thank you so much I really appreciate it. I’m doing my best to learn but it’s all new to me as I’ve never been taught properly how to code. Are you able to help me out with the high scores then I think my game is finished! I just need to crack these scores.

try debugging. Put a debug point in just before where you think you have the problem, run the game & then step into the code line by line & see what values are being passed. That will give an indication & you can work back up through it to see where your problem is.
Debug is your friend - helpful but can be a pain in the butt sometimes.

Thanks so much! I tried this and it worked brilliantly! I really appreciate it! I think I’m finally starting to understand PlayerPrefs. I owe ya one!