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
0
Question by Brosiscreations · Feb 03, 2021 at 01:49 PM · playerprefsif statement

PlayerPrefs with if statement not working

Hi, I'm quite new to unity and I can't figure out why this isn't working, I've use a very similar code elsewhere with no issues. I am trying to set the PlayerPrefs int only if the number is higher, for example if the Playerpref is already 3(highest) then I don't want it to change the amount to 2 or 1. The number is being set regardless of if it is higher or lower, does anyone know why or if there is a different way to code this? I've included part of the script because it's pretty long, it all works except the && 2 >PlayerPrefs.GetInt("Level1"). Any help would be much appreciated.

 IEnumerator Star()
     {
         currentLevel = SceneManager.GetActiveScene().buildIndex;       
         if (lives >= 2 && lives < 4 && livesHolder.activeSelf|| lives2 >= 2 && lives2 < 4 && livesHolder2.activeSelf)
         {           
             if (currentLevel == 1 && 2 >PlayerPrefs.GetInt("Level1"))
             {
 
                 PlayerPrefs.SetInt("level1", 2);
             }

,

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
2

Answer by CodesCove · Feb 03, 2021 at 09:40 PM

PlayerPrefs are case sensitive. You are getting "Level1" and setting "level1" and that might cause your issue. Would be better to define the strings like public const string LEVEL1 = "Level1"; (if they are globally used and constant values). then you can use it like PlayerPrefs.SetInt(LEVEL1, 2); That way you can avoid these typo errors.

PS. const is static in nature so you can call it from other classes via [LevelClassName].LEVEL1

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 Brosiscreations · Feb 04, 2021 at 11:00 AM 0
Share

Oh I knew that, can't believe I made such a silly mistake. You have no idea how long I've looked at that, tried changing things and I just didn't notice that at all. It's sorted now after changing the capital L, I can't thank you enough!

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

115 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make a script work in scene1 only if a collider in scene2 is triggered 1 Answer

PlayerPref/PlayerPrefsX why not able to store boolean values? 1 Answer

Storing in PlayerPref based on timer 1 Answer

how autosave for reduce blood 1 Answer

Saving with PlayerPrefs? How it works? 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