Home > front end >  How can I stop a game object box collider triggering underneath a dropdown box UI element?
How can I stop a game object box collider triggering underneath a dropdown box UI element?

Time:11-13

How can I stop a game object box collider triggering underneath a dropdown box UI element?

When I select a dropdown list item, it also sends a click through to the game object beneath the dropdown list.

Obviously we only want one UI element getting clicked in this case.

enter image description here

CodePudding user response:

What you need to do is implement IPointerClick on the scripts attached (a series of these interfaces are located in UnityEngine.EventsSystems) on both UI and the box collider, using this both will use the same event system and it will be effectively blocked.

You will need to add a PhysicsRaycaster to the main camera.

CodePudding user response:

So it seems there is no "inbuilt" or elegant way to do this, so its workaround time: Here is what I came up with that works perfectly.

Put these two lines in your update () to set a boolean flag marking the drop box in use.

  if (MyDropBox.transform.childCount != 3 && !MyDropBoxDown) { MyDropBoxDown   =  true;}    // Detect dropbox down and set a bool flag
  if (MyDropBox.transform.childCount == 3 && MyDropBoxDown)  { MyDropBoxDown   =  false;}   // detect dropbox up and unset a bool flag

Then you can either disable the box collider usng this flag, or just drop out of the function called by the box collider, eg if (MyDropBoxDown) {return} so it doesn't run.

  • Related