- Home /
Time based score system?
I'm completely new to Unity. I'd like to know how I can click an object and receive 5 points for every 5 seconds after that point. Once the object is clicked again it should no longer render points. Help please?
Answer by liamcary · Mar 26, 2013 at 05:31 AM
Store a timer, increment it if the object has been clicked, every 5 seconds add 5 points. Here's a basic example in c#.
 bool isCounting = false;
 float timer = 0f;
 int points = 0;
 
 public void OnObjectClicked()
 {
     isCounting = !isCounting;
 ]
 
 void Update()
 {
     if(isCounting)
     {  
         timer += Time.deltaTime;
         if(timer > 5f)
         {
             points += 5;
             timer -= 5f;
         }
     }
 }
 
 void OnGUI()
 {
     GUI.Label(new Rect(0,0,50,50), points.ToString());
 }
if i use your script, there is always a 0 on top of the "real" number... so you cant read the number of the score please help
Answer by roman_sedition · Nov 17, 2016 at 07:18 AM
You can probably use a self iterating coroutine.
     public void OnMouseOver()
     {
         
         if (Input.GetMouseButtonDown (0) && getPoints == false)
         {
             getPoints = true;
             StartCoroutine ("FivePointsCounter");
         }
         else if (Input.GetMouseButtonDown (0) && getPoints == true)
         {
             getPoints = false;
             StopCoroutine ("FivePointsCounter");
 
         }
 
     }
 
 
     IEnumerator FivePointsCounter()
     {
         points += 5;
 
         yield return new WaitForSeconds (5f);
         StartCoroutine ("FivePointsCounter");
 
     }
 
Your answer
 
 
             Follow this Question
Related Questions
Does Unity provide a means to determine the time since the last user interaction? 2 Answers
C# Interacting scripts via GetComponent 1 Answer
Calculate the distance a object has moved. 3 Answers
creating a timed function 3 Answers
What's the best way to calculate time passed since last playing app? 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                