DontDestroyOnLoad, where is my error?

Hello,

I’m new on Unity, so to start I’m on a little project of creating a hidden object games. I’m currently doing a prototype with the key feature. I find information (specially here) and manage to have invisible button and other stuff.

I now want to work some puzzle and other stuff that need to save data. So I’m creating, to start, a simple system: when I’m using a button to load a new scene, it adds one to a variable. Once I went twice on the same page, it’s over and the end scene is automatically loaded. For that, I try to use DontDestroyOnLoad, but it seems that I am facing an issue I can’t find. Here is the code, have you any idea?
The script is assigned to an empty object, and I have the expected messages on the console.

What is expected:
When I switch from the scene “cuisine” to the scene “chambre”, I have a variable "chambre_count " that counts how many times I went to this scene (and there is the same system for cuisine with cuisine_count).
When I went twice on the scene “chambre”, it loaded another scene “fin”.

The issue:
cuisine_count and chambre_count are always rebooted when I change scene and back to 0. Which means the user is never going to the scene “fin”, even if yes going 50 times on the scene chambre.

Here is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Button_Behavior : MonoBehaviour {

private int cuisine_count;
private int chambre_count;
private bool initialized;

void Start()
{

}

void Awake()
{
    Debug.Log("First Lol");
    if (initialized)
    { 
        Destroy(gameObject);
        Debug.Log("If Lol");
    }
    else
    {
        GameObject.DontDestroyOnLoad(gameObject);
        initialized = true;
        Debug.Log("Else Lol");
    }
    Debug.Log("Last LOL");
}

public void ChangeScene(string sceneName)
{

    Addtry(sceneName);
    Application.LoadLevel(sceneName);
    OverGame(sceneName);

}

void Addtry(string sceneName)
{
    if (sceneName == "chambre")
    {
        chambre_count = chambre_count + 1;
        print("Nombre de fois dans la chambre " + chambre_count);
    }
    else if (sceneName == "cuisine")
    {
        cuisine_count = cuisine_count + 1;
        print("Nombre de fois dans la cuisine " + cuisine_count);
    }
}

void OverGame(string sceneName)

{ 
    if (chambre_count == 2)
        {
            sceneName = "fin";
            ChangeScene(sceneName);
        }
}

}

Cheers,
Mathieu

Keep the variables static or use Playerprefs for them to be persistent

private int cuisine_count;

    private int chambre_count;

    private void Awake()
    {
        DontDestroyOnLoad(this);

        if (FindObjectsOfType(GetType()).Length > 1)
        {
            Destroy(gameObject);
        }
    }

    private void ChangeScene(string sceneName)
{
    if (sceneName == "chambre")
    {
        chambre_count += 1;
        print("Nombre de fois dans la chambre " + chambre_count);
        SceneManager.LoadScene(sceneName);
    }
    else if (sceneName == "cuisine")
    {
        cuisine_count += 1;
        print("Nombre de fois dans la cuisine " + cuisine_count);
        SceneManager.LoadScene(sceneName);
    }

    if (chambre_count == 2)
    {
        SceneManager.LoadScene("fin");
    }
}