Author Topic: OnHover firing on on each mouse click?!?  (Read 3440 times)

sotirosn

  • Guest
OnHover firing on on each mouse click?!?
« on: September 08, 2013, 04:10:43 AM »
Hello I have a script and I use..

void Awake() {
   UICamera.fallThrough = gameObject;
}

to pick up everything that the UI misses. Problem is that..

void OnHover(bool isOver) {
  ...
}

is receiving [isOver=true] whenever the mouse is clicked, as opposed to only when the mouse enters/leaves the 3D world.

luckily the OnHover is fired immediately after OnPress so I can do this work around..

bool isPressed;

void OnPress(bool isDown) {
   isPressed = true;
}

void OnHover(bool isOver) {
   if(isPressed) {
      isPressed = false;
      return;
   }
}

This unexpected behavior is a bug right?  It took me a few hours to track it down as it was not breaking my code, only making it act strangely in certain cases.

anexpert

  • Guest
Re: OnHover firing on on each mouse click?!?
« Reply #1 on: September 08, 2013, 06:47:48 AM »
I had same problem. Then I click on button I hear click and hover souns at the same time.  So I made this fix. Not perfect solution, but at least now then I click on button only click sound is played.
  1. private bool clicked = false;
  2.         // Use this for initialization
  3.         void Start () {
  4.        
  5.         }
  6.        
  7.         void OnHover (bool isOver)
  8.         {
  9.                 if (enabled && isOver && !clicked )
  10.                 {
  11.                         MasterAudio.PlaySound("Button","Hover");
  12.                         Debug.Log("Hovered");
  13.                         clicked = true;
  14.                 }else {
  15.                 clicked = false;       
  16.                 }
  17.         }
  18.  
  19.        
  20.  
  21.         void OnClick ()
  22.         {
  23.                 if (enabled )
  24.                 {
  25.                         MasterAudio.PlaySound("Button","Click");
  26.                         Debug.Log("Clicked");
  27.                         clicked = true;
  28.                 }
  29.         }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: OnHover firing on on each mouse click?!?
« Reply #2 on: September 08, 2013, 07:46:52 AM »
When button is clicked on, the typical event sequence is always:

Hover -> Press -> Click -> Hover

It was done this way from the very beginning in order to make state changes trivial for buttons. Whether it's the best way to go or not, it's much too late to change it at this point.