Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

mouse click gets catched twice

Discussion in 'Scripting' started by gabrielh, Apr 20, 2008.

  1. gabrielh

    gabrielh

    Joined:
    Feb 24, 2008
    Posts:
    51
    Hi!

    I bring up a little GUI element on left mouse click like so:

    void OnGUI(){
    ...
    else if(Input.GetButton("Fire1"))
    {
    showGUI = true;
    }
    ...
    if (showGUI == true)
    {
    showMyGUI();
    }
    ...
    }


    This, works so far. But getting rid of the GUI again, is only possible if I query for "Escape". What I would like is: click once -> GUI up, click second time -> GUI down again.
    When I put this into code than a click is received twice for every real click! So, the GUI just blinks and disapears again...
    Any ideas what I could do?

    Thanks!
    Gabriel
     
  2. Doleman

    Doleman

    Joined:
    Aug 12, 2005
    Posts:
    87
    Try to use

    Code (csharp):
    1. if(Input.GetButtonDown("Fire1")){
    2.   // Do stuff
    3. }
     
  3. gabrielh

    gabrielh

    Joined:
    Feb 24, 2008
    Posts:
    51
    makes the situation a bit better, but still no solution. the code:

    else if(Input.GetButtonDown("Fire1"))
    {
    if (showGUI == true)
    {
    showGUI = false;
    Debug.Log("to false");
    }
    else
    {
    showGUI = true;
    Debug.Log("to true");
    }
    }

    produces on ONE click:

    to true
    to false
    to true
    to false

    so, 4 calls instead of 1...
     
  4. gabrielh

    gabrielh

    Joined:
    Feb 24, 2008
    Posts:
    51
    moving the whole thing to Update() (instead of OnGUI()) gives me two calls. getting closer...
     
  5. gabrielh

    gabrielh

    Joined:
    Feb 24, 2008
    Posts:
    51
    this seems really weird... can someone try this on his installation. Code is simply:

    void Update(){
    if (Input.GetMouseButtonDown(0))
    {
    Debug.Log("catch a mouse press");
    }
    }

    then instantiate the script on an object, run and click ONCE. For me the debug log gives the mouse press twice, but this would be in conflict with what the doc says. I already checked if I have several instances of the script in the scene, but this is not the case...

    Thanks!
    Gabriel
     
  6. gabrielh

    gabrielh

    Joined:
    Feb 24, 2008
    Posts:
    51
    ok, digging a bit deeper showed me that also Start() is called twice... seems to be something wrong with my scene. Any ideas what it could be?

    Thanks!
     
  7. gabrielh

    gabrielh

    Joined:
    Feb 24, 2008
    Posts:
    51
    sorry, found it. the script was instantiated twice...
     
    eRh likes this.
  8. BurtGalaxy

    BurtGalaxy

    Joined:
    May 3, 2013
    Posts:
    1
    Hey, I found this thread because I have the same problem. How did you find that the script was instantiated twice? I just have the script sitting on my camera as a component. It looks like it's just the one.
     
  9. tpainton

    tpainton

    Joined:
    Jun 4, 2014
    Posts:
    16
    This is an old reply but I had the same exact problem. I had the same script attached to more than one object. Had to go through and look at each gameObject and sure enough, there it was attached twice. Thus, I was getting two mouse clicks for every one click.
     
  10. Ereous

    Ereous

    Joined:
    Aug 29, 2012
    Posts:
    163
    You can get events in OnGUI but you need to use the Event API

    Otherwise use Update()
     
  11. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    In the hierarchy window, click the search field, type your full script name, and choose 'Type' from the dropdown menu. It'll show you only objects with that script attached.
     
  12. dreinzy

    dreinzy

    Joined:
    Apr 24, 2015
    Posts:
    3
    Exact same problem & solution, script attached twice. Thank you!
     
  13. HonoraryBob

    HonoraryBob

    Joined:
    May 26, 2011
    Posts:
    1,214
    I've been having the same problem, but I don't think I have multiple instances of any script running. What else would cause something like this? Would two clicks be registered if two different scripts (not the same type) are simultaneously looking for mouse clicks using Input.GetKeyUp(), Input.GetMouseButton() etc? I don't know why that would do any harm, but I don't know what these functions are doing "under the hood".
     
  14. libraryguyeli

    libraryguyeli

    Joined:
    Jan 13, 2022
    Posts:
    1
    I've been scratching my head at this all day and found out that I somehow attached my weapon script to my player two times? I would suggest anyone taking a look there first. Don't know how I did it. It was just in the Inspector of my player GameObject twice and I removed it once and it worked perfectly.