Loading Scenes & Load Player Data

When creating a game with multiple levels, it's important to carry over certain data, like the player's current level, weapons, health, and inventory, when moving between scenes. Simply loading a new scene won’t automatically transfer this data. Instead, you need to save the necessary information first, then load the new scene using SceneManager.

Here’s a simple example of how you can do that:

using UnityEngine;
using UnityEngine.SceneManagement;
using cowsins.SaveLoad;

// This script saves the game and then loads a new scene on TriggerEnter, which may
// be useful for Portals, for example.
public class LevelTransition : MonoBehaviour
{
    public string sceneToLoad;

    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            DataPersistenceManager.Instance.SaveGame();
            SceneManager.LoadScene(sceneToLoad);
        }
    }
}

Last updated