Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by CrossRiverGames · Jan 27, 2014 at 07:32 PM · dontdestroyonloadunity4.3not-working

DontDestroyOnLoad not working

I have a button in my game that allows the player to go back to the title screen mid-game. In order to save the score in such an occurrence, I put the score in the following class:

 using UnityEngine;
 using System.Collections;
 
 public class Score : MonoBehaviour {
 
     // This class contains the score and saving variables.
 
     public int intScore;
     public float decScore;
     public bool keepScore; // This is to determine whether to keep or throw out the old score upon starting the game.
 
     void Awake () {
         DontDestroyOnLoad(this); // This makes sure the score stays as long as the app is open
         if (FindObjectsOfType(GetType()).Length > 1)
         {
             Destroy(gameObject);
         }
     }
 }

The intention is that a false value in keepScore indicates that the game is over and thus the score should not be kept, whereas a true value indicates that the game is in progress and thus the score should be kept. When the game ends, the value is set to false with a simple

 score.keepScore = false;

(I am accessing it from another class) and when the game is in progress but the title screen is available to be selected the value is set to true with

 score.keepScore = true;

However, when I go back to the title screen using the following code:

         if (Input.touchCount == 1 && touched == false)
         {
             Application.LoadLevel("StartScreen");
         }


and then return to the game, the value is always false, even if I had set it to true. What am I doing wrong?

Comment
Add comment · Show 1
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 sirbranedamuj · Jan 27, 2014 at 08:02 PM 0
Share

Does this Score object already exist in the scene that you are loading? It could be re-initializing it somehow. Check the scene's object hierarchy when you load StartScreen. There could be a duplicate object.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by MagicoCreator · Jan 27, 2014 at 08:33 PM

if you want it to exist you can consider using static variables instead of singleton mechanisms

even though if you plan to stick to the singleton version, i would recommend checking the what scripts can set it to false, or if your start funtion can write it.

another guess is that you're using DontDestroyOnLoad(this) which only protects the instance of the class but if the object which holds it is destroyed it might get desroyed along with it, try

 DontDestroyOnLoad (transform.gameObject);
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 CrossRiverGames · Jan 30, 2014 at 07:13 PM 0
Share

Thank you for pointing out the solution that was right under my nose!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Update() method is not called 0 Answers

DontDestroyOnLoad not working in Unity 4.3 1 Answer

is it OK to use DestroyImmediate in game? 4 Answers

Singleton DontDestroyOnLoad data persistance 0 Answers

DontDestroyOnLoad problem 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