Search code examples
unity-game-enginedropdownunity-ui

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?

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


Solution

  • 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.