Reload scene without reseting variables

I want to reload a scene using SceneManager.LoadScene (). However, by doing this, my LifeCount is setting to MaxLife once again, since I have

void Start(){
LifeCount=MaxLife;
}

I need to mantain this line (you always start with MaxLife), however, when reloading the scene, your LifeCount must be decreased by a unit. Any idea? I’ve seen static variables, and doesn’t work exactly how I want (maybe I don’t use them properly)

You need a GameManager. Also, using DontDestroyOnLoad.

Edit :

here is the example code for how to do this with a GameManager.

    using UnityEngine;

    //To be attached to an object inside the first scene
    public class GameManager : MonoBehaviour
    {
        private static bool _created = false;

        //Accessible only trough editor or from this class
        [SerializeField]
        private int maxLives = 5;

        public int livesLeft;

        private void Awake()
        {
            if (!_created)
            {
                DontDestroyOnLoad(this.gameObject);
                _created = true;
                Init();
            }
        }

        public void Init()
        {
            livesLeft = maxLives;
        }
    }

    public class PlayerMove : MonoBehaviour
    {
        private GameManager _manager;
        private int _lives;

        private void Awake()
        {
            //The following line should work if you stick to having one GameManager in the game
            _manager = GameObject.FindObjectOfType<GameManager>();
            _lives = _manager.livesLeft;
        }
    }

You can then add features to your GameManager such as saving the lives left in the PlayerPrefs. This is basics in code architecture, separate responsibilities. Your game objects shouldn’t all have to know how to save data from the scene, only one object has to do that, but the other objects need to have access to it.
@PauMat27

There are a couple of ways you can do this. You can either use DontDestroyOnLoad to make an object persistent between scenes or save the data on disk either using PlayerPrefs or serialization.

I prefer the first one, for that you just run the function DontDestroyOnLoad on Awake

void Awake()
{
    DontDestroyOnLoad(gameObject);
}

For PlayerPrefs you can check the documentation on how to save and load data Unity - Scripting API: PlayerPrefs

Hi, I think dontdestroyonload is a good solution for your problem, but you can also try “PlayerPrefs” in this scenario, PlayerPrefs are used for permanent storage of values.
if you don’t have knowledge of PlayerPrefs, try read this: