• 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
1
Question by Rs31412 · Feb 17, 2014 at 05:43 PM · timescorescore systemscoring

How to increase score by one per second when you are holding an object

Hello. I'm working on a game in which there are 2 ways of scoring. 1- 1point/sec while holding a ball. 2- 5 points if you drop the ball in a basket. I have multiple questions in this but I'd like to focus on the 1pt/sec part for now.

Here's my code (sorry it's pretty sloppy, I'm kind of new):

     public int score;
     private bool hasBall;
     private float startTime;
 
     void Start()
     {
         score = 0;
         hasBall = false;
         startTime = Time.time;
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.tag == "ball")
         {
             //Destroy(other.gameObject);
             other.gameObject.transform.parent = gameObject.transform;
             hasBall = true;
         }
         else
             hasBall = false;
 
     }
 
     void Update()
     {
         if (Input.GetKeyDown("space"))
             print("throwBall");
          
         if(hasBall)
             score = (int)(Time.time - startTime);
 
         Debug.Log("Score: " + score);
 
     }

This works for giving me 1 point per second, but it doesn't recognize when I pick it up as the start time. My debug log often will say I have "Score: 5" the first second I pick it 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

2 Replies

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

Answer by Maui-M · Feb 17, 2014 at 06:12 PM

Delta time will give you the time passed since the previous frame/update. Use that to get how long the ball has been held. Whenever its been held for over a second add that to the score and subtract it from your time counter.

     public int score;
     private bool hasBall;
     private float heldTime = 0.0f;
  
     void Start()
     {
        score = 0;
        hasBall = false;
     }
  
     void OnTriggerEnter(Collider other)
     {
        if (other.gameObject.tag == "ball")
        {
          //Destroy(other.gameObject);
          other.gameObject.transform.parent = gameObject.transform;
          hasBall = true;
        }
        else
          hasBall = false;
     }
  
     void Update()
     {
        if (Input.GetKeyDown("space")){
          print("throwBall");
          heldTime = 0.0f;
         }
  
        if(hasBall){
          heldTime += Time.deltaTime;
          if(heldTime >= 1){
             score += (int)heldTime;
             heldTime -= (int)heldTime;
          }
         }
  
        Debug.Log("Score: " + score);
     }
Comment
Add comment · Show 3 · 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 Rs31412 · Feb 17, 2014 at 11:53 PM 0
Share

Thank you so much for the quick reply! It works great! Wish I could upvote it but I don't have 15 rep yet =/ just started on this. Wish I could link this to my Stack Overflow account so I could.

avatar image getyour411 · Feb 17, 2014 at 11:59 PM 0
Share

@Rs31412 ,below the thumbs up/down icons, do you see another option to 'select this as answer'?

avatar image Rs31412 · Feb 18, 2014 at 12:05 AM 0
Share

just found it! Thanks! Need to get used to this site! Thanks!

avatar image
-1

Answer by CagatayAkyazi · Jan 26, 2019 at 12:55 AM

THANX a lot five years after but:))) it helped me a lot... thnx again

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 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

Related Questions

Associate time and distance for a score system 3 Answers

How to save score for survival time? 1 Answer

Score Multiplier by Time 1 Answer

Scoring System 3 Answers

I want my score to reset back to 0 but keep my highscore saved 3 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