Author Topic: Disable OnPress through collider?  (Read 5344 times)

decoy26517

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 12
    • View Profile
Disable OnPress through collider?
« on: October 29, 2013, 01:30:21 PM »
Hey there,

Here's what I'm trying to do: I have two colliders, one is a box and the other is the ground. The Box has a "selection" script that includes an OnPress that says that if it's clicked, it gets added to the selection array. The ground as a DEselection script that clears out the array.

The problem is that the click on the box is going through and also clicking the ground at the same time. So the box can only be added if it's not hovering over the ground.

Is it possible to prevent two OnPress events from happening if they are stacked on top of each other (so that only the top/first OnPress executes)?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Disable OnPress through collider?
« Reply #1 on: October 29, 2013, 04:20:13 PM »
NGUI events are consumed by the first collider, so I am guessing you're doing your own raycasts, which is where the problem comes from.

decoy26517

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 12
    • View Profile
Re: Disable OnPress through collider?
« Reply #2 on: October 29, 2013, 05:02:07 PM »
NGUI events are consumed by the first collider, so I am guessing you're doing your own raycasts, which is where the problem comes from.

No, I'm not using any raycasts.

The basic setup:


Object1 scripts:

  1. void OnPress(bool isDown)
  2. {              
  3.         UICamera.genericEventHandler = gameObject;
  4.         //Debug.Log(UICamera.currentTouch.current.collider.name);
  5.         if(isDown)
  6.         {
  7.                 if(UICamera.currentTouchID == -1)
  8.                 {
  9.                         SelectionManager.GetComponent<SelectedManager>().SelectedObject = gameObject;
  10.                         Debug.Log("Object1 Selected");
  11.                 }
  12.         }
  13.         }
  14.  


Object2 Script: (basically the same exact thing)

  1. void OnPress (bool isDown)
  2. {      
  3.         if(isDown)
  4.         {
  5.                         SelectionManager.GetComponent<SelectedManager>().SelectedObject = null;
  6.                         MoverShower.SetActive(false);
  7.                         Debug.Log("Object2 Selected");
  8.         }      
  9. }


If all you do is click the first object, nothing bad happens. It prints out correctly that Object1 was selected over and over again with each click. If you click off that object and onto Object2 you get the Object2 Selected print out. BUT if you then go back and click back on Object1, it prints out both "Object1 Selected" "Object2 Selected".

1 Odd thing: if you set the console to collapse, it groups all similar print outs. There's a  "Object1 Selected" print out and TWO  "Object2 Selected". 1 populates when Object1 is clicked (which it shouldn't), the other populates when Object2 is clicked.

The information at the bottom of the console is almost exactly the same with one difference between the two One is:
"UICamera:Notify(GameObject, String, Object) (at Assets/NGUI/Scripts/UI/UICamera.cs:734" the other:
"UICamera:Notify(GameObject, String, Object) (at Assets/NGUI/Scripts/UI/UICamera.cs:730"

Looking at UICamera I'm guessing its telling me I'm not using a generic event handler for the second click?
Any ideas on what this could be?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Disable OnPress through collider?
« Reply #3 on: October 29, 2013, 05:07:24 PM »
Our line numbers don't match. Please update to the latest version before posting.

And yes, if you set the generic event handler, you will start receiving a copy of the events. This is not how it was meant to be used. Generic event handler should be set in OnEnable() and removed in OnDisable(), and it should be done so on some manager script.

What you should be doing instead is moving focus from the object you started dragging to the object you want to receive events (as I assume that's what you are trying to do? not sure why else you'd be setting the generic event handler)

decoy26517

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 12
    • View Profile
Re: Disable OnPress through collider?
« Reply #4 on: October 29, 2013, 05:22:12 PM »
Our line numbers don't match. Please update to the latest version before posting.

Done. Still getting the same issue. UICamera.cs:762, UICamera.cs:758 are the new line #s though.

And yes, if you set the generic event handler, you will start receiving a copy of the events. This is not how it was meant to be used. Generic event handler should be set in OnEnable() and removed in OnDisable(), and it should be done so on some manager script.

What you should be doing instead is moving focus from the object you started dragging to the object you want to receive events (as I assume that's what you are trying to do? not sure why else you'd be setting the generic event handler)

Annnd yeah. I was messing around with the UICamera.genericEventHandler earlier and I guess I just kept over looking it. That was the problem and removing it fixed the issue.  ;)

Thanks.