How can i Double click on a gameObject?

I'm trying to enable double clicking on my gameobject to rotate it by a preset number of degrees, Looking in the docs it appears that the Event.current.clickCount property is just for use with GUI.

Here's a somewhat less complicated way:

using UnityEngine;

public class DoubleClick : MonoBehaviour {

    float doubleClickStart = 0;

    void OnMouseUp()
    {
        if ((Time.time - doubleClickStart) < 0.3f)
        {
            this.OnDoubleClick();
            doubleClickStart = -1;
        }
        else
        {
            doubleClickStart = Time.time;
        }
    }

    void OnDoubleClick()
    {
        Debug.Log("Double Clicked!");
    }

}

Note, the double-click sensitivity is determined by the 0.3f float value. This is the amount of time allowed between clicks to register a double click.

Also, the start time is re-set to a negative number when a double click is triggered, so that three clicks in quick succession does not trigger two double click events - they're only triggered in pairs.

I have one semi solution here which works currently, but the trapDoubleClicks function is called again while triggering the double click. There's got to be an easier way for this, but i couldn't find it in my searching!

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Collider))]
public class DoubleClickRotateObject : MonoBehaviour
{
    public float DoubleClickTimeout = 0.2f;
    public Vector3 DoubleClickRotateAmount = new Vector3(0, 0, 15);

    public bool PrintDebug=false;

    void OnMouseUp()
    {
       if (PrintDebug) print("mouse up on " + transform.name);
       StartCoroutine(trapDoubleClicks(DoubleClickTimeout));
    }

    IEnumerator trapDoubleClicks(float timer)
    {
        if (PrintDebug) print("Starting to listen for double clicks");
        float endTime = Time.time + timer;
        while (Time.time < endTime)
        {
            if (Input.GetMouseButtonDown(0))
            {
                if (PrintDebug) print("Double click!");
                transform.Rotate(DoubleClickRotateAmount);
            }
            yield return 0;
        }
        if (PrintDebug) print("no double click in " + timer + " amount of time");
        yield return 0;
    }
}

bool mouseClicksStarted = false;
int mouseClicks = 0;
float mouseTimerLimit = .25f;

public void OnClick(){
	mouseClicks++;
	if(mouseClicksStarted){
		return;
	}
	mouseClicksStarted = true;
	Invoke("checkMouseDoubleClick",mouseTimerLimit);
}

 
private void checkMouseDoubleClick()
{
	if(mouseClicks > 1){
		Debug.Log("Double Clickedd");
		
	}else{
		Debug.Log("Single Clicked");
		
	}
	mouseClicksStarted = false;
	mouseClicks = 0;
}

Solved

private bool clickEnable = true;
private bool doubleClick = false;
        
void OnMouseUp ()
        	{
        		if(clickEnable)	{
    			clickEnable = false;
    			StartCoroutine(trapDoubleClicks(DoubleClickTimeout));
    		}
    		
    	}
    
IEnumerator trapDoubleClicks(float timer)
        {
            Debug.Log("Starting to listen for double clicks");
            float endTime = Time.time + timer;
            while (Time.time < endTime)
            {
                if (Input.GetMouseButtonDown(0))
                {
                    Debug.Log("Double click!");
    				yield return new WaitForSeconds(0.4f);
    				clickEnable = true;
    				doubleClick = true;
                    
                }
                yield return 0;
            }
    		
    		if(!doubleClick){
            Debug.Log("no double click in " + timer + " amount of time");
    		  //single click
    		}
    		else{
    			doubleClick = false;
    		}
    		
    		clickEnable = true; 
            yield return 0;
        }

Solved

public float DoubleClickTimeout = 0.2f;
private bool clickEnable = true;
private bool doubleClick = false;

void OnMouseUp ()
	{
		if(clickEnable)	{
			clickEnable = false;
			StartCoroutine(trapDoubleClicks(DoubleClickTimeout));
		}
		
	}
    

IEnumerator trapDoubleClicks(float timer)
   {
        Debug.Log("Starting to listen for double clicks");
        float endTime = Time.time + timer;
        while (Time.time < endTime)
        {
            if (Input.GetMouseButtonDown(0))
            {
                Debug.Log("Double click!");
				yield return new WaitForSeconds(0.4f);
				clickEnable = true;
				doubleClick = true;
                
            }
            yield return 0;
        }
		
		if(!doubleClick){
        Debug.Log("Single click");
		
		}
		else{
			doubleClick = false;
		}
		
		clickEnable = true; 
        yield return 0;
    }

Not sure why people getting to so complicated solutions , just do like this :slight_smile:

    void OnMouseDown()
{
	if ((Time.time - doubleClickStart) <= 0.3f)
	{
		this.MyFuction();
		doubleClickStart = 0f;
	}
	else
	{
		doubleClickStart = Time.time;
	}
}

void MyFuction()
{
	
}

This one works, is simple, and will call a Single-Click method only if it doesn’t become a Double-Click.

float doubleClickStart = 0; //Last time a click was received

float doubleClickInterval = .3f; //max time in seconds for it to be considered a double click

public void OnMouseUp()   //Point to this with an OnMouseUpEvent or OnMouseClick
{
    if ((Time.time - doubleClickStart) <= doubleClickInterval ) 
 {  // <.3s so call Double-Click
        this.CancelInvoke("OnSingleClick");  //Cancel pending OnSingleClick
        OnDoubleClick(); 
        doubleClickStart = -1;
    }
 else  // >.3 seconds, so record the time, and schedule a single click call.
 {
        doubleClickStart = Time.time;
        Invoke("OnSingleClick", doubleClickInterval );  //Call OnSingleClick in .3s (unless canceled)
    }
}

void OnDoubleClick()
{

//Enter your Double-Click code here

    Debug.Log("Double Clicked!");
}

void OnSingleClick()
{

//Enter your Single-Click Code here

    Debug.Log("Single Clicked!");
}