Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Gamevara · Jul 31, 2015 at 02:54 PM · playerprefsscoredontdestroyonload

DontDestroyOnLoad reset the gameobject

Hi,

I'm having a flappy bird kind of game. I would like to have previous score shown on the screen when I die and start it all over. Somehow I don't manage to do so. However the best scores shows at it should (and all the other text element).

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class GameControlScript : MonoBehaviour 
 {
     public Text scoreText;                    
     public GameObject gameOvertext;               
     public Text lastScoreText;
     public Text bestScoreText;

 int bestScore = 0;
 int score = 0; 
 bool isGameOver = false;                             
 
 void Awake()
 {
     bestScore = PlayerPrefs.GetInt ("best", 0);
 }

 void Update()
 {
     //if the game is over and the player has pressed some input...
     if (isGameOver && Input.anyKey) 
     {
         //...start a new game.
         Application.LoadLevel(Application.loadedLevel);        
     }
     bestScoreText.text = "Best: " + bestScore;
 }

 public void Scored()
 {
     score++;
     scoreText.text = "Score: " + score;
 }

 public Died()
 {
     gameOvertext.SetActive (true);
     isGameOver = true;

     lastScoreText.text = "LAST: " + score;

     //check if high scored
     if (score >= bestScore)
     {
         PlayerPrefs.SetInt("best", score);
         PlayerPrefs.Save();
     }
 }

}

Then again I have an extra script for lastScoreText only which is named NoDestroy:

 using UnityEngine;
 using System.Collections;
 
 public class NoDestroy : MonoBehaviour {
    void Awake (){
       DontDestroyOnLoad (this.gameObject);
    }
 }

but it still reset my "last score" when I restart the level. I don't know why, but appreciate if someone could help me. I have also tried to put empty gameobject and place lastScoreText there, but the results are same. So the question is: How I can modify the code so it won't reset my lastScoreText when restarting the level?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by nullgobz · Jul 31, 2015 at 04:12 PM

Well your best score is using player prefs to save the score to the registry. So do the same with your last score?

 public class GameControlScript : MonoBehaviour 
 {
    int lastScore = 0;
 }
 void Awake()
 {
    lastScore = PlayerPrefs.GetInt ("last", 0);
 }
 public Died()
 {
     gameOvertext.SetActive (true);
     isGameOver = true;
  
     lastScoreText.text = "LAST: " + lastScore;
     PlayerPrefs.SetInt("last", score);
  
     //check if high scored
     if (score >= bestScore)
         PlayerPrefs.SetInt("best", score);
     PlayerPrefs.Save();
 }

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 Gamevara · Aug 01, 2015 at 08:18 PM 0
Share

Thanks for the reply.

Yes, that's a one option, but I thought it might be easier use just DontDestroyOnLoad.

Should I also save it (PlayerPrefs.Save) after setting the last score? Does that disturb also the best score field? Or would it replace only the changed field?

Is there a difference with the performance using PlayerPrefs or DontDestroyOnLoad?

Thanks again.

avatar image nullgobz · Aug 03, 2015 at 09:16 AM 0
Share

Yes if your using DontDestroyOnLoad you dont really need playerprefs. But since you started with playerprefs i thought you wanted to use it. PlayerPrefs.Save() will save all the values you have set, so you only need to call it once. I would use playerprefs only if you need to save the score across diffrent ga$$anonymous$$g sessions. And use the DontDestroyOnLoad method if you dident need to see the "last score" next time you start the game. About the performance im not sure.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Guide in ArrayPrefs or PlayerPrefx? 1 Answer

keep adding to playerprefs 1 Answer

score broken, score loads on new scene but then errors 0 Answers

GetComponent doesn't work 2 Answers

PlayerPrefs Highscore 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