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 adrianologyff7 · Nov 22, 2017 at 04:22 AM · scenescorelevelscore system

Cannot retrieve score from previous scene/level

Hello, I am new in unity and I'm currently developing a space shooter shmup for my school project. I have a script that gets the current score and high score then saves it. Here it is

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class ScoreManager : MonoBehaviour 
 {
     public int score;
     public int BestScore;
     public Text ScoreText;
 
     // Use this for initialization
     void Awake () 
     {
         ScoreText = GetComponent<Text> ();
         BestScore = PlayerPrefs.GetInt ("bestScore");
         score = 0;
     }
 
     // Update is called once per frame
     void Update () 
     {
         ScoreText.text = "Score: " + score;
         if (score > BestScore) 
         {
             BestScore = score; 
         }
         GameObject.Find("Score").GetComponent<Text>().text=""+score;
         GameObject.Find("Best").GetComponent<Text>().text=""+PlayerPrefs.GetInt("bestScore");
     }
     public void OnDestroy()
     {
         PlayerPrefs.SetInt ("bestScore", BestScore);
         PlayerPrefs.Save ();
     }
 }
 


Now the problem is that when I proceed to another scene, the current score returns to zero but the high score remains as it is which is fine but I need to retrieve the score from the previous scene to the current scene then continue gaining score as I destroy enemies just like in similar games. I have tried using DoNotDestroyOnLoad and PlayerPref.GetInt but it still doesn't work or perhaps I've made a mistake on my part. I have tried looking up for other solutions but still no luck so I thought I'd ask here. Thank you in advance.

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 jeffreyrampineda · Nov 22, 2017 at 04:38 AM 0
Share

DontDestroyOnLoad() should work. $$anonymous$$aybe you're resetting the score to zero from another script when you load different scenes?

avatar image adrianologyff7 jeffreyrampineda · Nov 22, 2017 at 12:47 PM 0
Share

You were right! I just found out that my game_manager script is resetting it to zero as I load to the next level. Here it is. score.GetComponent<Score$$anonymous$$anager>().score = 0;But now I can't seem to figure out how I would transfer the score here and DontDestroyOnLoad() gives me a "DontDestroyOnLoad() only work for root gameobjects" message.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JedBeryll · Nov 22, 2017 at 05:38 AM

There are a few ways to do this. One is the DontDestroyOnLoad as jeffreyrampineda said, but it might cause problems if you designed your game differently. I think the best way for you is a static value.

 public class ScoreManager : MonoBehaviour 
 {
     public static int lastScore; //static value so it does not get reset when the manager is reloaded
     public int score;
     public int BestScore;
     public Text ScoreText;
 
     // Use this for initialization
     void Awake () 
     {
         ScoreText = GetComponent<Text> ();
         BestScore = PlayerPrefs.GetInt ("bestScore");
         score = 0;
     }
 
     // Update is called once per frame
     void Update () 
     {
         ScoreText.text = "Score: " + score;
         if (score > BestScore) 
         {
             BestScore = score; 
         }
         GameObject.Find("Score").GetComponent<Text>().text=""+score;
         GameObject.Find("Best").GetComponent<Text>().text=""+PlayerPrefs.GetInt("bestScore");
     }
     public void OnDestroy()
     {
         PlayerPrefs.SetInt ("bestScore", BestScore);
         PlayerPrefs.Save ();
         lastScore = score; //set lastScore at the end of the level
         //you can now use this when another level is loaded
     }
 }

Note that this solution wont work if you quit the game. In that case you will have to save it in playerprefs too.

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 adrianologyff7 · Nov 22, 2017 at 02:33 PM 0
Share

Thank you for the reply. Sadly, it still doesn't work for me but I want to try saving it on playerprefs but where would be the best place to put it?

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

85 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

Related Questions

New score each level 1 Answer

GetComponent doesn't work 2 Answers

Allow userto load custom unity levels/scenes. 2 Answers

Enter next level 1 Answer

How to set a game-over scene when an object hits another? 0 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