- Home /
Problem with colliders when turning isTrigger on / off
I am making a moonlander in unity where I am able to jump in and out of the moonlander. When i jump out I perform this code on the moonlander.
if( rigidbody.IsSleeping() )
{
collider.isTrigger = true;
gameTrackerLink.moonlanderExited();
rigidbody.isKinematic = true;
Debug.Log( "exited vehicle" );
}
Basically I activate my character, and set the moonlander's cockpit to a trigger, to be able to jump in again. Then when i jump in again i run this code:
if( other.name=="Character" )
{
gameTrackerLink.moonlanderEntered();
collider.isTrigger = false;
rigidbody.isKinematic = false;
Debug.Log( "entered vehicle" );
}
The problem is that somehow a child collider (the landing gear in this case), starts acting as a trigger, though the editor says it's not! Here's a build with the problem:
http://dl.dropbox.com/u/2610712/UnityAnswers.rar //arrow keys, down when on ground exits vehicle
I've located the problem in the code, and commenting away "collider.isTrigger = false;" will remove the bug (but the cockpit is still a trigger). I can't figure out what the real cause is, or if I can work around it. Any help is highly appreciated!
I have no OnCollision - methods, the first block of code is run in update at the press of a button, and the second block is all there is in OnTriggerStay.
Have looked around a little, and this problem seems to have been around for a while ( http://forum.unity3d.com/viewtopic.php?t=61777&highlight=collider+istrigger ), so i put a bounty on it
I've just tried this in your build and I think I can reproduce your problem. After leaving and returning, the lower box no longer acts as collider. If you can post the project, I'd have a look and see if I can fix this ... with the information we have, it's kind of difficult. You could also send me a P$$anonymous$$ on the Unity forums, if you wish to keep it more private.
Answer by Ashkan_gc · Sep 01, 2010 at 03:07 PM
as a short term solution you can change the isTrigger property of the children. it seems that it's a bug. hopefully, it's not available in unity 3. i recommend reporting it.
example code to change the children's properties
javascript
for (var child : Transform in transform)
{
child.collider.isTrigger = false;
}
C# code
foreach (Transform child in transform)
{
child.collider.isTrigger = false;
}
technically speaking transform is an enumerable object. it returns the transform and all of it's children in iterations of the foreach loop.
if you need to change properties in components that they are not accessible in transform, you should use GetComponent.
I think I basically just elaborated on what you had already said - sorry about that.
no problem :) i voted you up! two answers is better than no answer :)
Answer by Thom Denick · Sep 01, 2010 at 06:14 PM
I have run into this problem (trigger-once) before when I use Plane Colliders. If you are using a Plane Collider in this example, simply change the Collider to a Cube type collider, make it fairly thin, and see if it will trigger multiple times.
Answer by swhite · Sep 04, 2010 at 12:37 AM
That's annoying bug.
have you tried simply iterating through all the children one parent collision and forcing their trigger off? (no idea if this'll work in the bugged state)
if( other.name=="Character" ) { gameTrackerLink.moonlanderEntered(); collider.isTrigger = false;
for (var child : Transform in transform) {
child.collider.isTrigger = false;
} rigidbody.isKinematic = false; Debug.Log( "entered vehicle" ); }
Might be worth a shot to see if you can force it out of a trigger state. If not you could consider attaching a separate gameobject to your spaceship that acts as the trigger at all times.
no problem man! two answers is better than no answers :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613072112im_/https://answers.unity.com/themes/thub/images/avi.jpg)