Click counter
Hey guys, Im trying to do a clicking counter which the counter read and records every click i clicked. Any ideas?
what do you want to use it for? do you just want it to record every time it is clicked??????
Answer by BerggreenDK · Jul 22, 2011 at 10:01 AM
You will need a few things I guess:
1) a counter
2) something to click on or some way of detecting the input
Lets start out:
Instead of going into colliders or other hardcore examples, lets use the build in GUI button as trigger.
First we need a counter.
int counter=0; // there we have a variable for the counter
Then we need to show a button that can be clicked. To be lazy we also use the text label on the button to show how much we have counted.
void OnGUI()
{
if (GUI.Button(new Rect(100,100,200,50), "Count: " + counter))
{ // the IF is true = clicked, lets count one
counter ++;
}
}
Put these into a new ClickCounter.cs file:
You can copy/paste this:
using UnityEngine;
using System.Collections;
public class ClickCounter : MonoBehaviour
{
int counter=0; // lets start with zero
void OnGUI()
{
if (GUI.Button(new Rect(100,100,200,50), "Count: " + counter))
{ // the IF is true = clicked, lets count one
counter ++;
}
}
}
@BergGreenD$$anonymous$$ Thank you. I was attempting something similar and now it's working.
Why is it that
if (Input.GetButtonDown ("Fire1")) {
total = int1 + 1;
Debug.Log (total);
Only returns "2" per click but
if (Input.GetButtonDown ("Fire1")) {
total ++;
Debug.Log (total);
Adds 1 each time per click? wouldn't me stating "+ 1" add an extra digit each time?