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 kensai01 · Jan 31, 2016 at 02:25 PM · c#unity5update function

Having trouble increasing level inside of Update() when score reaches 1000...

Problem is that I can't figure out how to make the program increment the level once when the score reaches 1000 but then stop and not increment it again until the user reaches 2000. Currently Update() calls it again because the user doesn't have enough time to score another point before all the levels have been incremented. How would I instrument the below code to increment the level once when user hits 1000 points but then not again right away like it's doing now?

     // incrementing levels every 1000 points 
     if(PlayerPrefs.GetInt("CurrentScore") % 1000 == 0 && PlayerPrefs.GetInt("CurrentScore") != 0)
     {
         print(PlayerPrefs.GetInt("CurrentScore")); //for debugging
         Invoke("incrementLevel", 0);
         //incrementLevel();
     }


Level Increment Function:

 //function to increment level
 private void incrementLevel()
 {
     if (level <= (int)levels)
     { level++; }
 }



UPDATE, SOLVED:

I solved my own problem by sleeping and waking up with an idea. I'm sure this is how it was going to be suggested to me anyhow, I feel stupid for even asking the question now. Let me know if this is an inefficient way of doing it though.

First I created an int tempScore variable set to 0. (not shown)

Then I simply checked after the user hit 1000 points if currentScore is larger than tempScore before allowing the increment.

If it was larger, increment would happen then tempScore would be assigned to currentScore so that it doesn't go in again until the score is 2000.

     // incrementing levels every time person scores 1000 points
     if(PlayerPrefs.GetInt("CurrentScore") % 1000 == 0 && PlayerPrefs.GetInt("CurrentScore") != 0)
     {
         if (PlayerPrefs.GetInt("CurrentScore") > tempScore)
         {
             if (level < ((int)levels-1))
             {
                 print(PlayerPrefs.GetInt("CurrentScore"));
                 incrementLevel();
                 tempScore = PlayerPrefs.GetInt("CurrentScore");
             }
         }
     }
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

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Spawn added objects into a circle 1 Answer

how to do on single click fire and on drag appears the joystick and get the input from joystick 0 Answers

Movement Unit (RTS) 0 Answers

Car from Unity's standard assets giving me of null reference errors 0 Answers

How do I resolve, get_isActiveAndEnabled can only be called from the main thread error 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