• 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 sahilsimransingh100 · Nov 08, 2020 at 10:42 AM · level loadlevel editorlevel selectlevelinglevel-editor

Level PlayerPrefs wont increase after a game

public class LevelPrefs : MonoBehaviour { public Transform playerPrefRef; public Text textPrefRef; int lvlTextInt = 1; bool incrementBool = true; int i;

 private void Start()
 {
     incrementBool = true;
     if (playerPrefRef.position.z <= -86 && incrementBool)
     {
         PlayerPrefs.SetInt("Current Int", lvlTextInt += 1);
         incrementBool = false;
         //lvlTextInt++;
     }
 }

 private void Update() 
 {
     textPrefRef.text = PlayerPrefs.GetInt("Current Int", lvlTextInt).ToString();

 }

 void playerPrefsMeth()
 {
     PlayerPrefs.SetInt("Current Int", lvlTextInt);
 }

 public void resetPrefs()
 {
     PlayerPrefs.DeleteKey("Current Int");
 }

} it at first the level of the game is 1 then after it reaches the end the int becomes 2 but afterwards it doesn't increment at all if you play the game again or keep playing because after you reach the finish line it resets and it starts with 2 now but when it finishes it doesn't becomes 3 or stack up.

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
Best Answer

Answer by jackmw94 · Nov 08, 2020 at 06:41 PM

It's hard to debug without a bit more context but a few clues here are:

As far as I can see, your playerPrefsMeth function never gets called meaning you only ever set that player pref in Start.

Also your Start function is the only thing that increments the levelTextInt and since it always starts at 1, this value will only ever be either 1 or 2. Even if you create new ones, these new ones will initialise the levelTextInt to 1.

I expect you want a function that you call when you complete a level that will be used to increment your levelTextInt:

 public void LevelCompleted()
 {
     levelTextInt++;
     playerPrefsMeth(); // (to update the player prefs)
 }


If you don't have one already, use an IDE that attaches to Unity so you can use breakpoints, then you can see when/if you hit various parts of your code and what the values being used are.

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 sahilsimransingh100 · Nov 10, 2020 at 05:07 AM 0
Share

so I'm going to have a start method that checks an if statement and after sometime the game has been played and the conditions have been met the code in the if statement will execute, right? which will consist of making the bool false and calling the method that you gave. in the method it increments the integer and is it just suppose to call the playerPrefs$$anonymous$$eth? without any parameter of anything or any changes here and there? Sorry I'm really new to this

avatar image jackmw94 sahilsimransingh100 · Nov 10, 2020 at 09:59 AM 1
Share

We've all been new at some point!
Because all this code is synchronous, it'll all happen at the moment that it's called. So if you want something to happen after some time once certain conditions have been met, that code should not be in Start since everything in Start will only happen at the Start of this component (i.e. when it first gets loaded when the scene starts OR if you generate it, then when it gets created). Instead, put that code in Update so it can be checked every frame - then it will run as soon as all conditions are met.

If you can give me a little more information about what else calls this script then I think I'll be able to help a bit better: Is it playerPrefRef.position.z <= -86 that deter$$anonymous$$es whether the level is complete? Does your game always start at level 1? Or are you using player prefs to start where they left off? What are you trying to do with 'incrementBool'?

avatar image sahilsimransingh100 jackmw94 · Nov 12, 2020 at 03:31 AM 0
Share

Thanks for spending time on this. this script is attached on the game manager and in the script I reference an object cause it moves on the z axis and if that object reaches a certain point (playerPrefRef.position.z <= -86) then the game has finished and I want to just increment an integer in the playerprefs but idk it just wont and here is the deal there is only one level scene when the level if finished the same scene is loaded meaning the level restarts which is exactly what i need and that integer increasing after every level passed. i tried the update method too instead of start but tend to increment the int every frame so in implemented the increment bool variable so at default it is true meaning it has permission to increment so it only checks for playerPrefRef.position.z <= -86 and when the conditions are met increment player pref integer and make increment bool variable false so it only gets called once in the update method. hope this helps.

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Load scene transistions 1 Answer

I am making a level editor and need ideas on how to save the level file. 1 Answer

How can I lock a Level? 2 Answers

How to generate levels in unity from ogmo editor? 0 Answers

Problems with unlocking levels 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