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 26, 2016 at 07:14 PM by technano for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by technano · Sep 08, 2016 at 06:23 AM · scoredontdestroyonloadscore system

Don't understand how to use DontDestroyOnLoad.

So I've been trying to understand how to get DontDestroyOnLoad to work for a while now and I'm still confused. I do understand that you can use it to make data persist through the transition of scene changes which is exactly what I'm trying to do. I just want to make the players score to continue to the next scene with the score they ended the first scene with. I've also tried messing around with PlayerPrefs but I found out that that's not really what I should be using for this kind of thing. Here is my code for the score:

 using UnityEngine;
 using System.Collections;
 
 public class ScoreManager : MonoBehaviour
 {
     public GUIText scoreText;
     public static ScoreManager Instance;
 
     private int score;
 
     void Awake()
     {
         if (Instance == null) {
             DontDestroyOnLoad (gameObject);
             Instance = this;
         } else if (Instance != this) 
         {
             Destroy (gameObject);
         }
     }
 
     // Use this for initialization
     void Start ()
     {
         score = 0;
         ScoreUpdate ();
     }
 
     public void PlayerSave()
     {
         ScoreManager.Instance.score = score;
     }
 
     public void AddScore(int newScoreValue)
     {
         score += newScoreValue;
         ScoreUpdate ();
     }
     
     // Update is called once per frame
     void ScoreUpdate ()
     {
         scoreText.text = "Score: " + score;
     }
 }
 

I thought this would work, and I know how the singleton pattern works (at least I think.) but what happens when I play the game is after the first point I can't gain anymore points, and then going to the second scene the score restarts back to 0 and once I collect the first point the game stops working. If someone could please help me with this I would greatly appreciate it! And if anyone could help explain these processes to me or show me where to look for a good explanation that would be very helpful, Thank you!

Also here is my ScoreManager script. Just in case this could be useful.

Comment
Add comment · Show 3
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 Trevdevs · Sep 09, 2016 at 05:16 AM 0
Share

This might be a part of the issue, where is the void AddScore() being called and passed the parameter?

avatar image 5c4r3cr0w · Sep 09, 2016 at 06:44 AM 0
Share

Change your code in Awake() to following :

 if (Instance != null && Instance != this) {
             Destroy (gameObject);
             return;
         }
             
         Instance = this;
         DontDestroyOnLoad (gameObject);
avatar image Bunny83 5c4r3cr0w · Sep 09, 2016 at 06:53 AM 0
Share

This does exactly the same but is not necessarily more readable. The original code is easier to read as it has one condition per if statement and each statement handles a seperate case.

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Sep 09, 2016 at 06:49 AM

There are a lot things unclear about your setup. First of all what's the point of "PlayerSave"? If "ScoreManager" is a singleton there should only be a single instance of this class at any time. So "copying" the score of the "current" ScoreManager to the one stored in the instance variable makes no sense.

Next thing is: DontDestroyOnLoad only works on root gameobjects. So if you use it on a nested gameobject it has no effect at all. When the parent is destroyed all childs will be destroyed as well.

Another possible problem is your reference to the "GUIText". If this GUIText is not part of the singleton instance (so it doesn't persist) you will end up with a missing reference exception.

A minor thing you should fix: Remove the comment on UpdateScore. Far worse than an uncommented code is wrongly commented code. Comments should clarify things when something is difficult to understand. Good code doesn't need any comments because it's clear what it does. When something isn't clear you can add a comment to tell others (or yourself) why you did this or that. A wrong comment does the exact opposite. It confuses / misleads the reader. So code that is actually clear to understand gets harder to understand or is even misinterpreted.

Comment
Add comment · Show 2 · 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 technano · Sep 15, 2016 at 01:07 AM 0
Share

Thank you so much for answering first of all and sorry for the late reply, but I looked at it and it looks like you're right in your second point, where the "GUIText" isn't part of the singleton instance. So I checked what happens to the score manager in the scene and it stays there but the GUIText is deleted. How would I make it apart of the singleton instance? Also thanks again for the help!

avatar image technano · Sep 15, 2016 at 11:17 PM 1
Share

I feel very silly right now but I have the GUIText in the singleton instance now and everything works!! Thank you so much!!!

avatar image
-1

Answer by D3mon1zA · Sep 09, 2016 at 05:44 AM

You have :

 {
          if (Instance == null) {
              DontDestroyOnLoad (gameObject);
              Instance = this;
          } else if (Instance != this) 
          {
              Destroy (gameObject);
          }

it should be (I think) this:

 {
          if (Instance == null) {
              DontDestroyOnLoad (gameObject);
              Instance = this;
          } else if (Instance != null) 
          {
              Destroy (gameObject);
          }

sou your else if should be asking if it is not equal to null instead of not equal to this. I think the reason for this is if 2 instances do occur they will delete each other? I'm not entirely sure on if that is correct or not (I have only just learn't this myself) but give it a shot see how you get on also try adding:

 using.System;
 using.System.IO



this video Might help https://www.youtube.com/watch?v=yxziv4ISfys

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 Bunny83 · Sep 09, 2016 at 06:31 AM 0
Share

Sorry but that makes no sense.

Awake is only called once per object. So the first instance that executes this code will see that there is no instance yet and makes itself "the" instance. The second (or third, or any other) instance will see that there is already an instance. If that one instance isn't themselfs they destroy themselfs.

Your condition ( Instance != null ) would allow that the actual singleton can destroy itself if somehow the code is executed twice.

Also importing System or System.IO is completely nonsense. You would only import those namespaces when you need any class from those namespaces. Also in Unity it's generally better to avoid importing System as it has some collisions with the UnityEngine namespace. Apart from that even $$anonymous$$icrosoft suggests to simply use the full qualified classname when you only need it one or two times. importing a namespace only makes sense when you need multiple things from a namespace.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Save/Add score while different level load 1 Answer

How can I make my score counter increase incrementally and not jump from 0 to 100? 2 Answers

game issues with infinte collisions 2 Answers

Scoring Points. 1 Answer

How do I stop adding to a variable? 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