Author Topic: On Hover not working properly  (Read 4417 times)

jrush64

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
On Hover not working properly
« on: July 18, 2017, 12:49:21 PM »
Hi,

I'm new to NGUI. I'm trying to make a sound play when my mouse hovers over an object. I got it working in a way. When the mouse is over the object, the sound doesn't play however when I move the mouse away from the object the sound plays.

This is the code I'm using.

  1. void Update()
  2. {
  3.  
  4.                         if (UICamera.hoveredObject == UserName)
  5.                                 OnHover (true);
  6.                         if (UICamera.hoveredObject == Password)
  7.                                 OnHover (true);
  8.                         if (UICamera.hoveredObject == SaveCheckbox)
  9.                                 OnHover (true);
  10. }
  11.  
  12. void OnHover(bool isOver)
  13.                 {
  14.                         if (isOver) {
  15.                                 HoverSound.Play ();
  16.                         }
  17.                         else
  18.                         {
  19.                                 HoverSound.Play ();
  20.                         }
  21.                 }

toyob

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 7
    • View Profile
Re: On Hover not working properly
« Reply #1 on: July 18, 2017, 04:03:40 PM »
You should do it in a completely different way. Put an UIEventTrigger script on each of the GameObjects you need to trigger the hovering sound on.
It has an OnHoverOver event. It should point to another script containing a simple function that plays the sound.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: On Hover not working properly
« Reply #2 on: July 22, 2017, 02:43:49 PM »
Having an Update() function that checks for the hovered object is a very wrong way to do it. NGUI sends out an OnHover event to your widgets with a collider automatically. There is no need for that Update function.