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 tfrago01 · Apr 30, 2013 at 05:29 AM · scorenumberincrease

Score that ticks up

Hi,

Ive got a score that i want to increase at a certain speed so that if i say have a score 0 than add 100 to it, the score wont immediately show 100 but tick up for 0 to 100.

Any ideas on how to do this?

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 asafsitner · Apr 30, 2013 at 05:44 AM

Using a **`Coroutine`** comes to mind. You can wait for a time, then 'tick' the score. For example:

 //this example increments the score by 1 every 1 second
 public class ScoreTicker : MonoBehaviour
 {
     //this is what our score should be like after we're done 'ticking'
     public int TargetScore;
 
     //this is the value representing our 'real' score at any given time
     public int CurrentScore;
 
     //this is how much we increment the score every tick - set in the inspector
     public int ScorePerTick = 1;
 
     //this is how long we wait between ticks - set in the inspector
     public float TickInterval = 1;
 
     void Start()
     {
         //in this case, as in most cases, I start the coroutine using it's name, so it can be stopped later
         StartCoroutine("Ticker");
     }
 
     //raise the score target
     public void AddScore(int score)
     {
         TargetScore += score;
     }
 
     //this will increment the CurrentScore towards the TargetScore over time
     public IEnumerator Ticker()
     {
         //this loop will run forever so you can just call AddScore and the ticker will continue automatically
         while (true)
         {
             //we don't want to increment CurrentScore to infinity, so we only do it if it's lower than TargetScore
             if (CurrentScore < TargetScore)
             {
                 CurrentScore += ScorePerTick;
 
                 //this is a 'safety net' to ensure we never exceed our TargetScore
                 if (CurrentScore > TargetScore)
                 {
                     CurrentScore = TargetScore;
                 }
             }
 
             //wait for some time before incrementing again
             yield return new WaitForSeconds(TickInterval);
         }
     }
 }
 
Comment
Add comment · 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

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

14 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

Related Questions

How to make an icon appear randomly 1 Answer

every x points, object speed increases 1 Answer

Using images to show numbers 1 Answer

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

how to i increase a number temporally 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