Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Sep 23, 2017 at 01:11 PM by golivrob for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by golivrob · Sep 22, 2017 at 10:19 PM · scene-loadingdontdestroyonload

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MacDx · Sep 23, 2017 at 01:30 AM 0
Share

But what's the problem? What's the difference between want you want to happen and what is actually happening?

avatar image golivrob MacDx · Sep 23, 2017 at 07:04 AM 0
Share

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.

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by JamesRebello · Sep 23, 2017 at 11:47 AM

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image golivrob · Sep 23, 2017 at 01:06 PM 0
Share

Hi James,

Thanks a lot, this is working! Having " private static int cuisine_count; " and " private static int chambre_count; " is enough. :)

avatar image
0

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");
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image golivrob · Sep 23, 2017 at 01:05 PM 0
Share

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.

Follow this Question

Answers Answers and Comments

71 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple EvenetSystems in Scene - only have 1 after searching though 1 Answer

Execution order of Destroy() and DontDestroyOnLoad() between Scenes 0 Answers

Don't destroy On Load make object not interactable in new scene 0 Answers

Losing prefab transform values on scene open 2 Answers

returning to scene & keeping the Camera 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges