- Home /
Enable Specific Collider when Another is Triggered.
The scene begins with a single object containing Collider A that is disabled, and Collider B that is enabled as a trigger. I need to enable Collider A when Collider B is triggered. Is there a way to look for a specific collider through scripting?
Answer by Armored13 · Jan 26, 2014 at 01:01 PM
You can attach a script to Collider B that uses OnCollisionEnter(). Make sure to leave a variable in that script for accessing Collider A; either a GameObject or a var depending on C# or Javascript. Then inside of Collider B's OnCollisionEnter(), have it use a SendMessage() to the referenced object.
You should either first check if the receiving object is null or send your message to not require a receiver:
if(ColliderA != null) {
ColliderA.SendMessage("ToggleActive");
}
or
ColliderA.SendMessage("ToggleActive", SendMessageOptions.DontRequireReceiver);
Hi Armore, thanks for the reply. Sorry I forgot to mention that both colliders reside in the same object. I have put the colliders into their own respective gameobjects. The scene now works with code similar to your answer but would it be possible to have both colliders in the same container and then selectively activate them?
If you have code that works with them being separate, you should be able to put them both under the same parent and have it work fine.
How do you mean selectively activate them? Are you looking to have them toggle activation each time they hit each other? Or making sure that only one collider can be turned on?
The current setup works just as I need it to. I just thought it would probably help with optimisation to have the colliders in a single object as opposed to having 2 game objects contained by another.