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

JavaScript count how much a button is pressed in 1 second

Discussion in 'Scripting' started by andrew-furlyk, May 17, 2015.

  1. andrew-furlyk

    andrew-furlyk

    Joined:
    Jun 7, 2014
    Posts:
    27
    said in the title i need some basic code witch i cant figure out well i need is that when a button is pressed it counts how much the button was clicked in 1 second.
     
  2. proandrius

    proandrius

    Unity Technologies

    Joined:
    Dec 4, 2012
    Posts:
    544
    Something like this:
    Code (csharp):
    1.     private int count = 0;
    2.     private bool firstHit = true;
    3.     private bool started = false;
    4.     private bool isEnabled = true;
    5.    
    6.     private void OnGUI(){
    7.        if(GUI.Button(new Rect(10,10,150,80),"Click Me "+started)){
    8.            if(started==false&&isEnabled==true){
    9.                if(firstHit==true){
    10.                    started = true;
    11.                    count = 1;
    12.                    firstHit = false;
    13.                    StartCoroutine(CountClicks());
    14.                }
    15.            }
    16.            else if(started&&isEnabled){
    17.                 count++;
    18.                 Debug.Log("CLICK: "+count);
    19.            }
    20.        }
    21.     }
    22.    
    23.     private IEnumerator CountClicks(){
    24.        yield return new WaitForSeconds(5.0f); // time for clicks
    25.        isEnabled = false;
    26.        Debug.Log("TOTAL CLICKS: "+count);
    27.        yield return new WaitForSeconds(1.0f); // pause
    28.        isEnabled = true;
    29.        firstHit = true;
    30.        started = false;
    31.     }
    (Haven't tested)
     
    Last edited: May 17, 2015
    calmcarrots likes this.
  3. andrew-furlyk

    andrew-furlyk

    Joined:
    Jun 7, 2014
    Posts:
    27
    the code has alot of errors and i dont know where to start even fixing it :( so could you like test it out and make it work cus i realy need it
     
  4. proandrius

    proandrius

    Unity Technologies

    Joined:
    Dec 4, 2012
    Posts:
    544
    Ok give me one sec, I will compile it.
     
  5. andrew-furlyk

    andrew-furlyk

    Joined:
    Jun 7, 2014
    Posts:
    27
    thanks
     
  6. proandrius

    proandrius

    Unity Technologies

    Joined:
    Dec 4, 2012
    Posts:
    544
    It had only one error. :) Fixed my post above. Next time trying fixing it yourself to learn coding. :)
     
  7. andrew-furlyk

    andrew-furlyk

    Joined:
    Jun 7, 2014
    Posts:
    27
    its wierd i keep getting errors here see Untitleddd.png Untitleddd.png
     
  8. proandrius

    proandrius

    Unity Technologies

    Joined:
    Dec 4, 2012
    Posts:
    544
    You have probably inserted it wrong into your code. Full script file should look like this (make sure file name is called "testscript"):
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class testscript : MonoBehaviour {
    6.  
    7.     private int count = 0;
    8.     private bool firstHit = true;
    9.     private bool started = false;
    10.     private bool isEnabled = true;
    11.      
    12.     private void OnGUI(){
    13.        if(GUI.Button(new Rect(10,10,150,80),"Click Me "+started)){
    14.            if(started==false&&isEnabled==true){
    15.                if(firstHit==true){
    16.                    started = true;
    17.                    count = 1;
    18.                    firstHit = false;
    19.                    StartCoroutine(CountClicks());
    20.                }
    21.            }
    22.            else if(started&&isEnabled){
    23.                 count++;
    24.                 Debug.Log("CLICK: "+count);
    25.            }
    26.        }
    27.     }
    28.      
    29.     private IEnumerator CountClicks(){
    30.        yield return new WaitForSeconds(5.0f); // time for clicks
    31.        isEnabled = false;
    32.        Debug.Log("TOTAL CLICKS: "+count);
    33.        yield return new WaitForSeconds(1.0f); // pause
    34.        isEnabled = true;
    35.        firstHit = true;
    36.        started = false;
    37.     }
    38. }
    39.  
     
  9. andrew-furlyk

    andrew-furlyk

    Joined:
    Jun 7, 2014
    Posts:
    27
    now i know why it does not work. it does not work cus its in c# not javascript. i need it in javascript sorry
     
  10. Xoduz

    Xoduz

    Joined:
    Apr 6, 2013
    Posts:
    135
    Here is the same code, in JavaScript.
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. public class testscript extends MonoBehaviour {
    4.     private var count : int = 0;
    5.     private var firstHit : boolean = true;
    6.     private var started : boolean = false;
    7.     private var isEnabled : boolean = true;
    8.    
    9.     private function OnGUI(){
    10.        if(GUI.Button(new Rect(10,10,150,80),"Click Me "+started)){
    11.            if(started==false&&isEnabled==true){
    12.                if(firstHit==true){
    13.                    started = true;
    14.                    count = 1;
    15.                    firstHit = false;
    16.                    StartCoroutine(CountClicks());
    17.                }
    18.            }
    19.            else if(started&&isEnabled){
    20.                 count++;
    21.                 Debug.Log("CLICK: "+count);
    22.            }
    23.        }
    24.     }
    25.    
    26.     private function CountClicks(){
    27.        yield WaitForSeconds(5.0); // time for clicks
    28.        isEnabled = false;
    29.        Debug.Log("TOTAL CLICKS: "+count);
    30.        yield WaitForSeconds(1.0); // pause
    31.        isEnabled = true;
    32.        firstHit = true;
    33.        started = false;
    34.     }
    35. }
     
    proandrius likes this.
  11. andrew-furlyk

    andrew-furlyk

    Joined:
    Jun 7, 2014
    Posts:
    27
    thanks dude. the code had some problems in the code but i fixed them :)
     
  12. Xoduz

    Xoduz

    Joined:
    Apr 6, 2013
    Posts:
    135
    Np. What were the problems, btw?
     
  13. andrew-furlyk

    andrew-furlyk

    Joined:
    Jun 7, 2014
    Posts:
    27
    lol i am fixing them and adding things that i need for the game
     
  14. andrew-furlyk

    andrew-furlyk

    Joined:
    Jun 7, 2014
    Posts:
    27
    and one of the problems where that you keept adding allot of private and public and all that stuff witch made the script not even work
     
  15. andrew-furlyk

    andrew-furlyk

    Joined:
    Jun 7, 2014
    Posts:
    27
    yaay i finaly fixed it