Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by pickledzebra · Jun 11, 2010 at 12:07 AM · onmouseupdouble-click

Anyone have a double-click script more reliable than mine?

Briefly, this script performs poorly enough to be non-viable. Any ideas for improvement or alternative approaches would be much appreciated.

var doubleClickStart = 0;

function OnMouseUp() {

 if ((Time.time - doubleClickStart) < 0.4)
 {
     this.OnDoubleClick();
     doubleClickStart = -1;
 }
 else
 {
     doubleClickStart = Time.time;
 }

}

function OnDoubleClick() {
// do some stuff.
}

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cyclops · Jun 12, 2010 at 06:48 PM 0
Share

$$anonymous$$inor point - consider using boolean variables, ins$$anonymous$$d of float.

avatar image pickledzebra · Jun 14, 2010 at 04:15 PM 0
Share

Good point, Cyclops. Thanks.

3 Replies

· Add your reply
  • Sort: 
avatar image
8
Best Answer

Answer by Mike 3 · Jun 11, 2010 at 12:23 AM

looks like you have a few minor bugs there, hopefully this will be a bit better

//start doubleClickStart as a float so no ambiguity to the compiler var doubleClickStart : float = -1.0; //EDIT TO DISABLE MOUSE CLICKS FOR A TIME AFTER DOUBLE CLICK var disableClicks = false; //END EDIT

function OnMouseUp() { //EDIT TO DISABLE MOUSE CLICKS FOR A TIME AFTER DOUBLE CLICK if (disableClicks) return; //END EDIT

 //make sure doubleClickStart isn't negative, that'll break things
 if (doubleClickStart > 0 && (Time.time - doubleClickStart) < 0.4)
 {
     this.OnDoubleClick();
     doubleClickStart = -1;
         lockClicks();
 }
 else
 {
     doubleClickStart = Time.time;
 }

}

//EDIT TO DISABLE MOUSE CLICKS FOR A TIME AFTER DOUBLE CLICK function lockClicks() { disableClicks = true; yield WaitForSeconds(0.4); disableClicks = false; } //END EDIT

function OnDoubleClick() {
// do some stuff.
}

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pickledzebra · Jun 11, 2010 at 12:41 AM 0
Share

Briliant! Perfect. Thanks for the corrections and the rapid response.

avatar image pickledzebra · Jun 14, 2010 at 04:16 PM 0
Share

$$anonymous$$ike, if you happen to read this, how would you ignore a 3rd rapid-fire click?

avatar image Mike 3 · Jun 14, 2010 at 04:54 PM 0
Share

modified the answer (and put comments around where)

avatar image pickledzebra · Jun 14, 2010 at 05:39 PM 0
Share

Great! Again, better than the one I tried to make. I'm learning... Thanks again. :)

avatar image Eowyn27 · Jan 08, 2014 at 04:27 PM 0
Share

Would like to see a C# version :P

Show more comments
avatar image
2
Wiki

Answer by adnirjhar · Jan 29, 2014 at 09:38 PM

C# version->

     private float doubleClickStart = -1.0f;
     private bool disableClicks  = false;
     void OnDoubleClick()
     {   
         Debug.Log("Double Click");
         // do some stuff.       
     }
 
     IEnumerator lockClicks()
     {
         disableClicks = true;
         yield return new WaitForSeconds(0.4f);
         disableClicks = false;
     }
     void Update()
     {
         if (Input.GetMouseButtonUp(0))
         {
             if (disableClicks)
                 return;
             
             if (doubleClickStart > 0 && (Time.time - doubleClickStart) < 0.4)
             {
                 this.OnDoubleClick();
                 doubleClickStart = -1;
                 lockClicks();
             }
             else
             {
                 doubleClickStart = Time.time;
             }
         }
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Jemaze · Sep 03, 2017 at 04:01 PM

Try This One! I Created A simple double click script. I hope it Helps!

 public bool firstClick=false;
 public bool secondClick = false;
 public int pressCount = 0;
 // Use this for initialization
 void Start () {
     
 }
 


 // Update is called once per frame
 void Update () {
     
     
     if(firstClick == true)
     {
         Debug.Log("Start Of Coroutine");
         StartCoroutine(doubleClickTime());
     }
     else
     {
         pressCount = 0; // the press count will reset after the end of the coroutine
     }

 }

 public IEnumerator doubleClickTime()
 {
     Debug.Log("Im Counting");
     yield return new WaitForSeconds(0.23f);
     if(pressCount >=2)
     {
         Debug.Log("Double Click!");
      // Do Some Awesome Things!
      
     }
     firstClick = false;





 }

 public void onClick()
 {
     firstClick = true;
     pressCount++;
   


 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to detect Double Clicking Horizontal/Vertical Axis on joystick 0 Answers

Mobile equivilent of onMouseUp 2 Answers

OnMouseOver Problem 3 Answers

Unity in Mac OS X trackpad clicking count problem. 0 Answers

OnMouseUP sometimes doesn't work 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges