Author Topic: How to check if an NGUI object is hit or not?  (Read 3369 times)

xikky

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 46
    • View Profile
How to check if an NGUI object is hit or not?
« on: June 18, 2013, 04:54:53 AM »
In order to check whether my "input touch script" should be executed or not, every FixedUpdate I'm checking the UICamera.hoveredObject result. I want that my script is only executed when UICamera.hoveredObject is null.

This is a simplified example of my code:

  1. function FixedUpdate () {
  2.        
  3.         if (Input.touchCount == 1) {
  4.                        
  5.                 if (Input.GetTouch(0).phase == TouchPhase.Began) {
  6.                        
  7.                         if (nguiCamera.hoveredObject != null)
  8.                                 word += nguiCamera.hoveredObject.ToString();
  9.                         else
  10.                                 word += "null";
  11.                 }
  12.         }
  13. }
  14.  

the string 'word' is displayed OnGUI.

The problem is that UICamera.hoveredObject result is a frame late.

So if I touch over a non NGUI object, word = null,
then touch over NGUI object, word = null,
then touch over NGUI object, word = not null,
then touch over non NGUI object, word = not null,
then touch over non NGUI object, word = null,

What am I doing wrong here? or is there another better solution to identify when an NGUI object is hit or not?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to check if an NGUI object is hit or not?
« Reply #1 on: June 18, 2013, 12:04:40 PM »
NGUI also updates its hovered object in FixedUpdate, so you have to adjust the script execution order to make yours execute right after it.

xikky

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 46
    • View Profile
Re: How to check if an NGUI object is hit or not?
« Reply #2 on: June 18, 2013, 12:40:02 PM »
Oh I see, I thought since UICamera is inside the plugins folder, UICamera.hoveredObject would be executed before my javascript files.

I fixed the script execution order by placing my "input touch script" at the bottom of all. When running the game using Unity Remote, hoveredObject works as expected. But when I build my game on android, again the hoveredObject works 1 frame late.

Am I missing something else? :/

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to check if an NGUI object is hit or not?
« Reply #3 on: June 18, 2013, 02:13:28 PM »
I'm not sure how the script order works on android. If you can't get it to work, you can always move your logic from FixedUpdate into regular Update. I'm not sure why you have it in FixedUpdate anyway. FixedUpdate is for physics.