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 kozankose · Oct 26, 2020 at 11:22 AM · scoreincreaseonceonlywhen

How can I increase the level Counter only once when player reached a certain amount of Score

Hello, I am new in unity and I would like to solve some problem.

There are 5 objects to be catched by player. Player will gain certain scores for catching each objects respectfully; Obj1=10; Obj2=20; Obj3=30; Obj4=40; Obj5=50; I would like to increase a counter when player encounters 1000 points every time.

To do this, I used mod operation like following;

     {
         
         if ( Score % 1000 <= 50) //50 is the largest score to be gained when player catch an object
         {
          
             levelCounter++;
             
             
         }
         

     }

However, I would like to increase the levelCounter only once when score is between 1000 and 1050. for example; if score is 1000 but you catched obj1 again, therefore since score will be 1010, "if" operation above will work again. Can you help?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Klarzahs · Oct 26, 2020 at 01:50 PM

Hi @Kozankose,

while using the modulo operator works in theory, you could also divide your score by thousand and use the resulting int value as a level indicator.
Code:

 levelcounter = (int)(Score / 1000);
 if(levelCounter > 50)
     //handle what ever happens then
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 kozankose · Oct 26, 2020 at 03:22 PM 0
Share

Dear @Klarzahs, I feel very shamed about how I missed that so easy solution. I thank you so much to opening my eyes..

avatar image
0

Answer by Bunny83 · Oct 26, 2020 at 02:10 PM

I would also recommend the solution that @Klarzahs posted. Though if you want to be able to increase the levelCounter independently from score (through other events, powerups, etc) you would essentially need an "edge detector" whenever you pass your desired score mark. For this you would need an additional state variable that you can use to create a proper delta value. You could just use an "oldScore" value that you set to the current Score at the end of Update.


This allows you to detect any rollovers at the moment it happened.

 if (Score % 1000 < oldScore % 1000)
 {
     levelCounter++;
 }
 oldScore = Score;

However this has several issues. Score increases which are around the order of magnitude as your desired limit or a multiple of that limit would not be detected. So if you award 1000 or 5000 points at once this breaks down. A better solution is to remember the levelincrease per 1000 score points seperately

 int levelBonus = Score / 1000;
 inf levelIncrease = levelBonus - oldLevelBonus;
 
 if (levelIncrease > 0)
     levelCounter += levelIncrease;
 
 oldLevelBonus = levelBonus;

This way no matter how Score changes for everyl 1000 points you get one additional level. Also you can add other level increases seperately from this.

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 kozankose · Oct 26, 2020 at 03:24 PM 0
Share

Dear @Bunny83, thanks for support. I will keep that in $$anonymous$$d.

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

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

Related Questions

Shoot only once when clicked 1 Answer

Do something only once 1 Answer

OnTriggerEnter only logs Debug.Log once, but is otherwise working fine 1 Answer

Playing an Animation only once Unity 4 0 Answers

How to make my espawn delay trigger to work more than once. 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