Reputation: 367
I have two identical 3D objects in my scene. Both of them have a boolean property: isSelected. For one of the objects, isSelected is true. For the other - isSelected is false.
Both objects have a script attached, which implements the IPointerDownHandler interface.
At some point in the game, both 3D objects have the same coordinates (absolutely the same coordinates, the objects overlap each other). If I click an object with my mouse, is there a way for me to predict for which object the OnPointerDown event will been triggered? I only want the OnPointerDown event to trigger for the object with isSelected == true.
Is there any way to achieve this? Should I use raycasts? Any help is appreciated, thanks.
Upvotes: 1
Views: 741
Reputation: 12332
Your question seems so easy I might misunderstand you!
if (!isSelected) return;
.. your handling code ..
Another idea is, in Unity you should really use components a lot, they are a natural part of things. Don't forget each of your c# scripts is indeed a "component".
Google "Unity3d disable a component" for a basic tutorial
yourComponent.enabled = false;
You say
I only want the OnPointerDown event to trigger for the object with isSelected == true.
Thus, perhaps just turn off that whole component with the boolean.
Another cool idea is this: turn off the collider with the boolean.
Collider c = GetComponent<Collider>();
c.enabled = false;
Then you truly won't get the event, you see?
Regarding your separate question
there a way for me to predict for which object the OnPointerDown event will been triggered
Since you're talking 3D objects,
1) If one is closer to the camera, that's the one
2) If they are in truly identical 3D positions, which should never happen: No, there's absolutely no way to tell, it's "random".
Upvotes: 2