1
\$\begingroup\$

I want to know how to detect if the user has released the mouse button the first time, or the times after that:

Pseudo-Code:

 if *first* (Input.GetMouseButtonUp(0))
   {
       do something
   }

 if *second, third, fourth..etc.* (Input.GetMouseButtonUp(0))
   {
       do something else
   }

I really have no idea on how to accomplish this. I'm sure it's pretty simple though!

\$\endgroup\$
  • \$\begingroup\$ You need to store the count in a separate variable yourself, then check the value of that variable. \$\endgroup\$ – Draco18s Jan 6 '16 at 18:22
  • \$\begingroup\$ @Draco18s I'm new to programming, I don't know how to do that sir! :) \$\endgroup\$ – user77178 Jan 6 '16 at 18:23
  • 1
    \$\begingroup\$ Programming with Variables \$\endgroup\$ – Draco18s Jan 6 '16 at 18:24
  • \$\begingroup\$ I see you have edited your question. The question appears to be a new question, rather than a rephrasing/clarification of the old question. If this is the case, you should ask a new question instead. If not, in what way are the current answers insufficient? \$\endgroup\$ – Draco18s Jan 7 '16 at 18:54
1
\$\begingroup\$

You should have private member in your class that will indicate if button was pressed for first time. You need to initialize it in starting function ( could be Awake, Start etc., read more about execution order in Unity ) as false. And then, in Update function - check it. If it is first time, the do X and set this var to true, else do Y.

using UnityEngine;
using System.Collections;

public class ClickerClass : MonoBehaviour {
    private bool m_clicked;

    void Awake() {
        m_clicked = false;
    }

    void Update() {
        if( Input.GetMouseButtonUp(0) )
        {
            if( m_clicked == false ) {
                m_clicked = true;
                // code for first time
            } else {
                // code for not first time
            }
        }
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Good answer although m_clicked would not need to be initialized and Awake() can be left out. The default of a boolean in C# will always be false. \$\endgroup\$ – Foggzie Jan 6 '16 at 19:11
0
\$\begingroup\$
int ClickCount;

void Update()
{    
    if (Input.GetMouseButtonUp(0))
    {
       if (ClickCount == 0)
       { 
              //First Click
              //do stuff
       }
       else 
       {
             //Not first click 
             //do stuff
       }

       ClickCount++;
    }
}

Keep in mind that the ClickCount variable will overflow once it reaches 2,147,483,648.

\$\endgroup\$
  • \$\begingroup\$ Comments are not for extended discussion; this conversation has been moved to chat. \$\endgroup\$ – Josh Jan 6 '16 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy