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 /
avatar image
0
Question by KChatzis06 · Mar 26, 2020 at 01:48 AM · destroydontdestroyonloadobject-reference-error

DontDestroyOnLoad still destroys object

I am creating a singleton inside Awake in order to keep one Game Session object at every scene. I am using DontDestroyOnLoad , but it doesn't seem to work. When I load into the next scene. Game Session gets destroyed and I lose all the data stored inside it. Here is my code : using UnityEngine;

 public class GameSession : MonoBehaviour
 {
 
     int score = 0;
 
     private void Awake()
     {
         SetUpSingleton();
     }
 
     private void SetUpSingleton()
     {
         int numberGameSessions = FindObjectsOfType<GameSession>().Length;
         if (numberGameSessions > 1)
         {
             gameObject.SetActive(false);
             Destroy(gameObject);
         }
         else
         {
             DontDestroyOnLoad(gameObject);
         }
     }

I check if there already is another Game Session object and if there is I destroy it. This is so that it keeps the original Game Session and not to reset the data that is stored inside it. Any help is greatly appreciated.

Comment
Add comment
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

2 Replies

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

Answer by dacarrera · Mar 26, 2020 at 05:35 AM

A few things, without getting too deep into whether a Singleton is even necessary here:

  1. With this setup, you're forced to include a GameSession GO in each of your scenes that want to use it, even if 99% of the time it'll be carried over from a previous scene.

  2. This isn't how a Singleton should be implemented.

  3. You're inheriting from MonoBehaviour without making use of it (aside from wanting the object alive in each of your scenes).


Singleton is a design pattern, so try not to think of it as "I'm creating a Singleton in Awake()" and rather that you're implementing a pattern that guarantees your object is unique and thread-safe. If you're just keeping track of int score, and want to use the Singleton pattern, you should do it directly instead of half-baking it with DontDestroyOnLoad(). Here's a quick implementation:

 // Thread-safe singleton implementation with lazy instantiation
 // You can throw your score field in here, and access it with GameSession.Instance.score
 public sealed class GameSession
 {
     private static readonly GameSession instance = new GameSession();
     static GameSession() { }
     private GameSession() { }
     public static GameSession Instance
     {
         get
         {
             return instance;
         }
     }
 }

When you implement it this way, you get rid of the unnecessary reliance on MonoBehaviour, you guarantee one unique object, and you'll be able to access your score Just by calling GameSession.Instance.score without worrying if there are multiple copies alive.

Comment
Add comment · 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
0

Answer by KChatzis06 · Mar 26, 2020 at 02:16 PM

@dacarrera Thanks a lot for the explanation. I will try to figure out how to use this way of implementing singletons. And one more question: for this to work, what namespaces do I have to be using?

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 dacarrera · Mar 27, 2020 at 01:28 AM 0
Share

You don`t have to use any!

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

131 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 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

How to stop duplicates from dontdestroyonload 1 Answer

Animator destroyed on DontDestroyOnLoad 2 Answers

How to make a saved score destroy on load new scene 0 Answers

"Dont destroy on load" does not work at forst 2 Answers

Object.DontDestroyOnLoad ? 2 Answers


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