0

I'm working on a 2D Pixel Platformer RPG, I need to develop a save and load mechanism in it. There are several scenes in the game (and will have many more), the question is, how do I save the scene number the player is currently in, so that when he quits and reloads the game, he's in the same scene. How can I implement it in C# Unity. (please be clear as I'm somewhat a beginner).

Sadeem Sajid
  • 65
  • 1
  • 8
  • Which part exactly are you having trouble with? You can get the currently active scene using the scenemanager: https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.GetActiveScene.html – UnholySheep Oct 27 '19 at 16:10
  • Possible duplicate of [What is the best way to save game state?](https://stackoverflow.com/questions/40965645/what-is-the-best-way-to-save-game-state) – Draco18s no longer trusts SE Oct 27 '19 at 17:32

1 Answers1

0

Ok, so there are a few things you need to do in order to achive this:

First, in the first scene in your build - create an Empty GameObject, name it "SceneManager".

Then, create a new tag "SceneManager" and add it to the "SceneManager" GameObject

Finally, add the "SceneManager" script to the "SceneManager" GameObject:

using System.Collections; 
using UnityEngine; 
using UnityEngine.SceneManagement;
public class SceneManager : MonoBehaviour 
{

    void Awake()
    {
        DontDestroyOnLoad(gameObject);
    }
    public void SaveScene()
    {
        int activeScene = SceneManager.GetActiveScene().buildIndex;
        PlayerPrefs.SetInt("ActiveScene", activeScene);
    }
    public void LoadScene()
    {
        int activeScene = PlayerPrefs.GetInt("ActiveScene");
        SceneManager.LoadScene(activeScene);
    }
}

Then, you can load/save scenes by using this script:

using UnityEngine;
public class UsageScript: MonoBehaviour { 
    private SceneManager SceneManager;
    void Awake ()
    {
        sceneManager = GameObject.FindGameObjectWithTag("SceneManager").GetComponent<SceneManager>();
    }

    void UsageManager()
    {
        sceneManager.SaveScene();

        sceneManager.LoadScene();
    }
}
  • Do not use PlayerPrefs to store game state. – Draco18s no longer trusts SE Oct 27 '19 at 17:29
  • @Draco18s why not ? – ChoopTwisk Oct 27 '19 at 17:33
  • 1
    1) `FileStream` is available 2) PlayerPrefs does not support complex data types 3) It is stored in plain text 4) It is intended for *preferences* such as volume level. – Draco18s no longer trusts SE Oct 27 '19 at 17:39
  • 1) Right, but why is it better in this case? Note that the question was about PlayerPerfs 2) I'm sending PlayerPerfs an int 3) I understand 4) from what I am reading here: https://docs.unity3d.com/ScriptReference/PlayerPrefs.html it looks like PlayerPerfs purpose was for things like saving the scene the player was in. @Draco18s –  Oct 27 '19 at 17:51
  • It is better *in general* not to use PlayerPrefs. You might only be saving a single integer *here* but somewhere along down the line someone will try and save entire game objects. *`it looks like PlayerPerfs purpose was for things like saving the scene the player was in`* I am not sure how you got that idea, all it says is "Stores and accesses **player preferences** between game sessions" with notes about where to find the file on each operating system and its list of methods. – Draco18s no longer trusts SE Oct 27 '19 at 18:12