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 /
  • Help Room /
avatar image
0
Question by DoubleD17 · Jul 03, 2017 at 09:54 AM · playerprefsif-statementsconversion

Can you help me do a highscore please? I've got this problem..

if (isdead == false) { score += Time.deltaTime * 25;

      scoreText.text = ((int)score).ToString();
      if (scoreText > PlayerPrefs.GetInt("Highscore"))
      {
          PlayerPrefs.SetInt("Highscore", scoreText);
      }
  } // I tried using score instead of scoreText in player prefs btw
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 dval · Jul 03, 2017 at 05:28 PM

You posted some bad code, and said you have a problem. It helps if you post the actual error from the the Unity console.

  1. you're trying to store the Text GameObject, not it's text value, this should be scoreText.text

  2. you've cast score as a string and trying to store value in int, you should store (int) score.

      //updated
      if (isdead == false) { 
               score += Time.deltaTime * 25;
               scoreText.text = ((int)score).ToString();
               if (score > PlayerPrefs.GetInt("Highscore"))
               {
                   PlayerPrefs.SetInt("Highscore", (int)score));
               }
         
           }
    
    
    

Additionally, I hope this is meant only to be updated after the character reaches end of level. Else, once your character reaches a high score, you start updating PlayerPrefs on each frame. This would kill performance and cause much needless activity.

Edit: I fixed the "operand > can't be used..." error. That was bad reading on my part, and maybe the root of the error.

  if (scoreText > PlayerPrefs.GetInt("Highscore"))  // bad

is trying to compare a GameObject is greater than an Integer. should have been:

 if (score > PlayerPrefs.GetInt("Highscore"))  // good

Which compares the int 'score' with the int 'Highscore'. This is something the computer can do.

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 DoubleD17 · Jul 04, 2017 at 09:25 PM 0
Share

That gives me an error in if (scoreText > PlayerPrefs.GetInt("Highscore"))

the error is operator > cannot be applied to operants of Text and Int

take it easy on me man im just a newbie in unity.. im starting, thats it.. and thanks btw :)

avatar image dval DoubleD17 · Jul 06, 2017 at 12:30 PM 0
Share

@DoubleD17 Sorry, I was having a grump day... I updated the example, was able to compare highscore with it. Hopefully the fixed example works for you.

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

109 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 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

Saving Highscore with PlayerPrefs not working 0 Answers

mathf.abs(0) is higher than 90, why? 1 Answer

Help with rotation please 2 Answers

How to use the button to call for a method 0 Answers

If statements with AudioListener C# ? 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