Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Updating score within timeframes

Discussion in 'Scripting' started by Bazoozoo, Nov 29, 2015.

  1. Bazoozoo

    Bazoozoo

    Joined:
    Mar 27, 2015
    Posts:
    28
    I'm looking to update my score once every time a player taps the screen within a certain timeframe.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class uiManager : MonoBehaviour {
    6.  
    7.     private int score;
    8.     public Text scoreText;
    9.     // Use this for initialization
    10.     void Start ()
    11.     {
    12.         score = 0;
    13.    
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update ()
    18.     {
    19.         if(Time.time > 0.8 && Time.time < 1.2)
    20.         {
    21.             if(Input.GetMouseButtonDown(0))
    22.             {
    23.                 score = score + 1;
    24.             }      
    25.         }
    26.         scoreText.text = ""+ score.ToString();
    27.            
    28.    
    29.     }
    30. }
    31.  
    How do I make it so when you press the mouse button down the first time it updates the score and any subsequent times it does nothing?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    add a boolean "alreadyTapped", set it to false at the start of the time window. Set it to true when the user taps and check "input && !alreadyTapped"
     
    Bazoozoo likes this.
  3. Bazoozoo

    Bazoozoo

    Joined:
    Mar 27, 2015
    Posts:
    28
    Is there a better way to choose multiple time intervals that allow an increase score rather than using many if statements?