- Home /
Should Physics.Raycast() hit a Trigger on a disabled GameObject?
Hi There
I have many UIs that occupy the same space on the screen and are hidden and shown as the game requires. Each button contains a collider (set to trigger) for that button and mouse/touch interaction is done via Physics.Raycast(). The problem I have is that when I am trying to click certain buttons that are visible, the Physics.Raycast() returns the Collider for a button who's GameObject is inactive. The UI is all in one hierarchy, the root of which contains a kinematic RigidBody with gravity off. If I remove this RigidBody, the 'hidden' colliders are no longer hit by the Raycast and it all works.
The docs are a little woolly on this - it says raycast is performed against all colliders (and presumably triggers) in the scene, and naturally there's the option to rayacst only against certain layers. It isn't explicit about behaviour against colliders on inactive GameObjects. I would however, have thought that the raycast would remain the same regardless of the RigidBody, unless the RigidBody has some intentional side-effect I am unaware of?
So what should the behaiour be? If the RigidBody is altering the behaviour, is this as expected? If so, can someone explain it, if not, can you post the link to log a bug :oD
I am using nGUI and have sought some guidance on the nGUI forums, but this feels more like a Unity issue? I am on the latest (3.6.6) Unity (Pro) and the latest nGUI. I have tried to re-create the issue in a smaller scene with no success as yet.
Thanks in advance. Bovine
Answer by Julien-Lynge · Oct 21, 2012 at 08:16 PM
That does sound like a bug - changing the RigidBody state changes the registration of the object with PhysX, but if a GameObject is inactive all of its components are inactive as well. Just curious - after the RigidBody has been registered with physics (e.g. after the Awake/Start function if you're adding in the editor, or a frame after you've added it at runtime), what happens if you turn on the deactivated trigger GOs and then turn them back off? Do they behave properly, or are they stuck in this mode?
You should definitely file a bug report - you can access it from the Help menu within the Unity Editor, and attach your project or specific files to the report.
If I toggle the state of the offending object, or turn it's Box Collider off, it then starts to behave correctly. Yes, it's sounding more and more like a bug - I'll log one.
A Rigidbody does change the behaviour of child colliders since they all belong to the rigidbody. They are kind of combined into one compound collider. See this page for more details.
Activating / deactivating a GameObject is not always a solution, some components seems like they aren't bound to the active state of the gameobject.
A collider is not a Behaviour, but it also has an enabled property. When you disable the collider it should work as you want.
While disabling the collider does work, this behaviour doesn't seem deter$$anonymous$$istic - if I create a simple UI with two buttons, one of which is hidden while the other is visible and visa versa, if the buttons are in the same place and each has a collider, I can merrily click the visible button and show the other button and then click that to show the first.
However somewhat randomly (although always the same button) the collider on a button who's GO is hidden will be returned by the raycast. It's random in the sense that it affects some buttons but not all. If I then turn the GO on and off for the hidden button, the collider for that hidden button is NOT subsequently returned by the raycast.
Just wanted to chime in and say I'm seeing the exact same behaviour - multiple nGUI panels, with box colliders overlapping in space, but only one set / tree of game objects active, and yet the correct box colliders aren't triggering. Remove the rigid body component from the top level game object shared by the various panels, and the correct colliders respond.
Your answer
Follow this Question
Related Questions
Unity 2D Trigger Detection is not working. 6 Answers
OnTriggerEnter mesh collider problem 1 Answer
Enemy line of sight using linecast and colliders 1 Answer
Rigidbody Trigger 1 Answer
Moving objects using raycasting? 1 Answer