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: Click counter - Unity Answers

Thank’s for reading me, every tips can help !

I DID IT ! I’ve created this script in C#, everytime you left click the counter add one :slight_smile:

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 ++; 
	}
}

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.)