Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    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?