[SOLVED] Increasing a variable from one scene to another

Hi,

I am beginner in Unity. Making my first game with at least 3 levels.

I want to display the level number on screen in-game. So, there’s this variable “CurrentLevelText” which displays CurentLevel value.

Problem: when i go from scene1 to scene2, I want to increase the value from 1 to 2 when the scene 2 is loaded… but I fail… :confused:

As I plan to add 50 levels, assigning manually a numer to each of the levels is not what I’m looking for.

I took some inspiration there:

Here’s the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class MainManager : MonoBehaviour
{
    public static MainManager Instance;

    public int CurrentLevel = 0;
    [SerializeField] public int NumberOfLife = 3; //not used yet
    [SerializeField] private TMP_Text CurrentLevelText;

    private void Awake()
    {
        CurrentLevelText.text = "Level " + CurrentLevel;

        if (Instance != null)
        {
            Destroy(gameObject);
            return;
        }

        Instance = this;
        DontDestroyOnLoad(gameObject);
    }

    private void Start()
    {
        if (MainManager.Instance != null)
        //if existss, it was already instanciated and therefore, we are in at least level 2
        {
            CurrentLevel++;
        }
        else
            //in this case, it was never instanciated and we set it to 1 (as it's 1st level)
            CurrentLevel = 1;
    }

}

As a result, CurrentLevel sticks on 0… I tried some other stuff but i was never able to increase it.
Any idea ?

Thanks

Hello.

You need some DontDestroyOnLoad object/script so you have variables and values that does not change when changing scene.

I suggest you to go look for tutorials about DontDestroyOnLoad and how to implement it.

Bye!