- Home /
Create a counter when "GetMouseButtonDown"
Hello everyone, I'm looking for this:
I want to have a counter on my Screen (Wich appears in GUI box, GUI Text or GUI Label) Everytime I press the left button of my mouse I want to add one to this counter. I know how to create a counter everytime you click on a GUI button it add one but i'm not looking for this: http://answers.unity3d.com/questions/147053/click-counter.html
Thank's for reading me, every tips can help !
If you need a simple click counter, did you try to make it with the counter script attached to a gameobject under the camera that covers all the screen ?
@$$anonymous$$iraSensei No I didn't, never eard/tried about it
It's juste an idea, I'm not sure it will work :)
Try to put a game object as a child of the camera (so your game object will always be just in front of it) without any graphic representation, but with a collider that will englobe any resolution of the camera. Then you can make the script to count and attach it to the game object.
I dont understand quite what you want. You want a GUI label/text that shows how many times you have clicked the left mouse button?
Seems pretty straight forward, you count every time you click the mousebutton by writing something like:
public int counter = 0;
void Update() {
if(Input.Get$$anonymous$$ouseButtonDown(0))
counter++;
}
And then have it show on the GUI. Idk how to do this with the new system, but in the old one used in the post you linked, its something like:
void OnGUI() {
GUI.Label(new Rect(100,100,200,50), "Count: " + counter));
}
@Lahzar I've just created the same script as you but thank's for the help :)
Answer by YetiStudio · Dec 29, 2014 at 08:57 PM
I DID IT ! I've created this script in C#, everytime you left click the counter add one :)
using UnityEngine;
using System.Collections;
public class Counter : MonoBehaviour {
int counter=0;
void OnGUI()
{
GUI.Label (new Rect (100, 100, 200, 50), "COUNTER: " + counter); {
}
}
void Update() {
if (Input.GetMouseButtonDown(0))
counter ++;
}
}
Diios, can you please select this answer as accepted, since it worked?
@Glurth No problem, thank you for re$$anonymous$$ding me
Answer by Glurth · Dec 29, 2014 at 08:58 PM
Create an Empty object, in the root of your scene called say “ClickCounter”, add a new script component to it, and open the script for editing. The code below shows how I detect if the mouse button has been depressed between calls to LateUpdate(). (Tutorials suggest using LateUpdate()
for this kind of stuff, rather than Update()
)
The code below does not provide details on how to actually DISPLAY the click value, just how to capture it. (The public ClickCounterTextScript TextField
variable is what I used in this case).
Note: This code does NOT use the event system built into unity, and does not require an EventSystem object be added to your scene. This also means if you want to “block” the counter, you will need to do so manually by adding code.
public class ClickCounter : MonoBehaviour {
public ClickCounterTextScript TextField;
private int clickCount;
private bool last_state;
void Start () {
clickCount = 0;
last_state = false;
}
void Update () {
bool state = Input.GetMouseButton (0);
if (state == false)
if (last_state != state)
clickCount++;
last_state = state;
TextField.SetText (clickCount);
}
}
EDIT: The Input.GetMouseButton(0) will return true, as long as the button is held down. Input.GetMouseButtonDown(0) will return true ONLY once for each click, regardless of how long it is held down. (This is the logic in my code above, and why the other answer, by diios, is better.)
Your answer
Follow this Question
Related Questions
Score/point counter system 1 Answer
C# countdown timer 9 Answers
Problem with GuiFont - Not appearing 0 Answers
Cube Counter 1 Answer
GUI.box (Score counter) doesn't appear when game is on play. 1 Answer