- Home /
The question is answered, right answer was accepted
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
But what's the problem? What's the difference between want you want to happen and what is actually happening?
Oh, I forgot that part! $$anonymous$$y bad...
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.
Answer by JamesRebello · Sep 23, 2017 at 11:47 AM
Keep the variables static or use Playerprefs for them to be persistent
Hi James,
Thanks a lot, this is working! Having " private static int cuisine_count; " and " private static int chambre_count; " is enough. :)
Answer by Positive7 · Sep 23, 2017 at 12:17 PM
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");
}
}
Hi Positive7,
Thanks for your help. I tried your code, but it's not working.
The " if (FindObjectsOfType(GetType()).Length > 1) " makes impossible to switch scene more than once. I put 150 to try but the count were back to 0 each time.
However, James has a perfect solution " private static int cuisine_count; " allows to protect the value so there is no need to use DontDestroyOnLoad.