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 Pulkit30 · Aug 12, 2018 at 08:08 AM · script.highscore

Score script not working , Wanting to show high score but can't. please help !!

My game goes with level and score I calculated with how much I go in z-direction . the following are scripts I used using System.Collections; using System.Collections.Generic; using UnityEngine;

public class CoreScore : MonoBehaviour { public Transform player; public static float x = 0f;

 public void Update()
 {
     x += player.position.z; 
    
 }

} using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement ;

public class Score : MonoBehaviour {

 public Transform player;
 public Text scoreText;
 public Text highScore;
  
  
 private void Start()
 {
    
     highScore.text = PlayerPrefs.GetFloat("Highscore", 0).ToString("0");
     CoreScore.x = PlayerPrefs.GetFloat("Highscore");
 }
 // Update is called once per frame
 void Update()
 {
    CoreScore.x = player.position.z;
      scoreText.text = CoreScore.x.ToString("0");
     highScore.text = CoreScore.x.ToString("0");
     /*  int buildindex = SceneManager.GetActiveScene().buildIndex;
       if (buildindex == 1)
       {

           scoreText.text = CoreScore.x.ToString("0");
       }
       if (buildindex >1||buildindex < 10)
       {

           scoreText.text = (CoreScore.x).ToString("0"); 
       }
       */
     if (CoreScore.x > PlayerPrefs.GetFloat("HighScore",0) )
     {
         PlayerPrefs.SetFloat("HighScore", CoreScore.x);
         highScore.text = CoreScore.x.ToString("0");
     }

 }

} please help me with this script thanks in advance :)

Comment
Add comment · Show 5
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 ShadowUser19 · Aug 12, 2018 at 08:24 AM 0
Share

Please fix your formatting so all code is correctly placed in the right code block. This will prevent your question from being skipped by some users. I wanted to fix your formatting myself but I think I don't have enough points yet.

avatar image ShadowUser19 · Aug 12, 2018 at 08:28 AM 0
Share

Also I see some commented code. What has the scene index to do with your problem. Please avoid adding unnecessary code. That way you keep your questions shorter and easier to read.

avatar image Pulkit30 ShadowUser19 · Aug 14, 2018 at 08:47 AM 0
Share

I was actually trying different way and commented it and then forgot to remove it.

avatar image Stratosome · Aug 14, 2018 at 09:40 AM 0
Share

Sorry, just trying to clarify, what exactly is the issue with this? How isn't the code working (I haven't tested it or anything, just asking)?

avatar image Pulkit30 Stratosome · Aug 14, 2018 at 10:11 AM 0
Share

It's that after every scene score resets to 0 . And it doesn't add last score in new scene

2 Replies

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

Answer by Stratosome · Aug 14, 2018 at 07:16 PM

Heyo,

Sorry I didn't respond sooner. Needed to sleep hah. Anyway, I think I know your problem. Up inside of your Start method, you are looking for a PlayerPref variable called 'Highscore' but below you are setting the PlayerPref variable 'HighScore', so that's a problem. They need to be called the same! I didn't see it right away and was trying to make a simple example of the whole thing just to see if I could get it to work. I'm pretty sure that's just your error, but if you'd like to see my version of the code anyway, here ya go:

 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 public class Score : MonoBehaviour {
     
     public static float x = 0.0f; // Your actual score
 
     private float lastScore = 0.0f; // The additional score from the last scene
 
     public Transform player;
     public string scoreText;
     public string highScore;
 
     private void Start() {
         // When scene is loaded, grab highscore from previous scene
         x = PlayerPrefs.GetFloat("Highscore", 0);
         lastScore = x;
 
         highScore = x.ToString("0");
         scoreText = highScore;
     }
 
     void Update() {
 
         if (player) {
             // Set score
             x = player.position.z + lastScore;
 
             // Update text
             scoreText = x.ToString("0");
             highScore = x.ToString("0");
 
             // Save new highscore if x is greater than the last
             if (x > PlayerPrefs.GetFloat("Highscore", 0)) {
                 PlayerPrefs.SetFloat("Highscore", x);
             }
         }
 
     }
 
 }
Comment
Add comment · Show 7 · 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 Pulkit30 · Aug 15, 2018 at 05:38 AM 0
Share

I thank you for your effort. Thanks for solving my problem

avatar image Stratosome Pulkit30 · Aug 15, 2018 at 08:10 AM 0
Share

'Course! Happy to help ya. At least it was just a simple little problem hah.

avatar image Pulkit30 Stratosome · Aug 15, 2018 at 08:22 AM 0
Share

one more problem is there , I wonder if u can help me . $$anonymous$$y android device recently upgraded to android oreo(8.0) . Before update my game was running on my device but after update , the game icon changed with unity logo icon and when I start the game, the message appears the game stopped working . I don't know why this error is occurring even though I have set to build at highest installed OS.

Show more comments
avatar image ShadowUser19 Pulkit30 · Aug 15, 2018 at 05:03 PM 0
Share

@Pulkit30 Ah, the lowercase uppercase problem. Its very easy to miss that.

Just a tip, but you could have used a constant for that. That way if you mistype something, then either the code editor or unity will start to point out your mistake.

avatar image Stratosome ShadowUser19 · Aug 15, 2018 at 10:23 PM 0
Share

Yep, that'd be a decent way to do it.

avatar image
0

Answer by ShadowUser19 · Aug 12, 2018 at 08:55 AM

I haven't used PlayerRefs yet, but I think you are supposed to use the Save method after making modifications:

 if (CoreScore.x > PlayerPrefs.GetFloat("HighScore",0) )
 {
     PlayerPrefs.SetFloat("HighScore", CoreScore.x);
     PlayerPrefs.Save();
     highScore.text = CoreScore.x.ToString("0");
 }

Also make sure the script is attached to a GameObject in Unity & that all the fields are set in the unity inspector.

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 Pulkit30 · Aug 14, 2018 at 09:13 AM 0
Share

No use, it did not work. I want it like as the scene changes after completing a level ,the last score adds to the next score which will update with player's z- position and when game is quit the high score is stored in the highscore variable

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

97 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

Related Questions

How to access my highscore from another scene ? 1 Answer

whats wrong with my high score code? 1 Answer

How can i call this void once 2 Answers

help understand @if false 1 Answer

problem when turning the object 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