Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by theTech · Nov 16, 2014 at 02:08 AM · scorelocalhigh

Local highscore issues

Hello, I'm here, once again, to ask for help.

I'm working on a local highscore for a game that I'm working on, but after spending the last few hours searching for a good solution, without any luck, i decided to ask.

The curent code that i got for the highscore script is the following:

 //Linking the text that needs to show the highscore
 public Text highscore;
 
 
 //If the score is better than the old highscore it needs to be replaced
         if (score > PlayerPrefs.GetInt ("highscore")) {
             PlayerPrefs.SetInt ("highscore", score);
         }
 
 
 // Get the highscore
 highscore = PlayerPrefs.GetInt ("highscore");
 
 //Set the text of the text.
 highscore.text = "Highscore: " + highscore;
 


My issue is:

Cannot implicitly convert type 'int' to 'UnityEngine.UI.Text'

And it leads to this line with the error: highscore = PlayerPrefs.GetInt ("highscore");

Is there any way to solve this issue, or maybe another way to make a local highscore?

Thanks.

Regards. Tech.

My full code:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class EndGameScore : MonoBehaviour {
 
     public Text Finalscore;
     public Text highscore;
       
     int score = 0;
     
     // Use this for initialization
     void Start () {
         
         if (score > PlayerPrefs.GetInt ("highscore")) {
             PlayerPrefs.SetInt ("highscore", score);
         }
                 highscore = PlayerPrefs.GetInt ("highscore");
                 score = PlayerPrefs.GetInt ("Score");
                 Finalscore.text = "Your score: " + score;
                 highscore.text = "Highscore: " + highscore;
                     
         }
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
Best Answer

Answer by Landern · Nov 16, 2014 at 02:10 AM

highscore is of type Text and GetInt from PlayerPrefs returns an int. You need to transform the type.

  //Linking the text that needs to show the highscore
 public Text highscore;
 //If the score is better than the old highscore it needs to be replaced
 if (score > PlayerPrefs.GetInt ("highscore")) {
   PlayerPrefs.SetInt ("highscore", score);
 }
 // Get the highscore
 // Set the text of the text.
   highscore.text = "Highscore: " + PlayerPrefs.GetInt("highscore").ToString();
 
 

EDIT:

Using your code, i'd probably modify it to do the following:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class EndGameScore : MonoBehaviour {
     
     public Text Finalscore;
     public Text highscore;
     int score = 0;
     int oldHighScore = 0;
     
     // Use this for initialization
     void Start () {
         oldHighScore = PlayerPrefs.GetInt ("highscore"); // You're already going to call this multiple times it seems, might as well stuff it in a variable.
         score = PlayerPrefs.GetInt ("Score");
         
         if (score > oldHighScore) {
             PlayerPrefs.SetInt ("highscore", score);
             PlayerPrefs.Save();
         }
         
         Finalscore.text = "Your score: " + score;
         highscore.text = "Highscore: " + (score > oldHighScore ? score : oldHighScore);
     }
 }

The part that you see setting the highscore.text is a ternary operator, it's short hand for a if/else statement, in the case above, if score is greater than oldHighScore, then use the value from score if not, use oldHighScore since it is still the king.

Comment
Add comment · Show 5 · 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 theTech · Nov 16, 2014 at 02:21 AM 0
Share

Hello, Thanks alot for the answer.

You did help me solve the issue, and for that, thanks alot.

But now I do have another question, seeing as it's currently staying at 0, i tried taking a few games to see if that made it update, however, there was no success.

Any suggestions / ideas on how this might be fixed?

avatar image Landern · Nov 16, 2014 at 02:26 AM 0
Share

are you calling PlayerPrefs.Save(); when you want to save them?

avatar image Landern · Nov 16, 2014 at 02:53 AM 0
Share

Added a bit more to chew on.

avatar image theTech · Nov 16, 2014 at 02:53 AM 0
Share

I wasen't at my brighetest moment before (Sorry, It's 4am)

After fooling around with it, i got it added, tried again a few times, however, it's still 0.

avatar image theTech · Nov 16, 2014 at 02:59 AM 0
Share

Oh, god!

Your updated post solved my issues right away, and that script works like a charm.

That's a pretty good way to get it updated on, guess you learn something new every day, as i tought that the other script did the same thing, but well, you learn something new every day.

Thanks a lot man!

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

High Score Name entry 1 Answer

How can i get the following script back on track to being a single level high score saver 0 Answers

Accessing local system ( File Browser ) 2 Answers

Load text from a file and disply it 0 Answers

Guitext for score over time. 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