I have an edge collider on my platforms and a gameObject attached to it with a trigger on it (box collider) when triggerEnter2D happens I’d like to disable the player.collider2D and when it leaves (OnTriggerExit2D) to enable it back.
void OnTriggerEnter2D(Collider2D Trig){
player.collider2D.enabled = false;
}
void OnTriggerExit2D(Collider2D Trig){
player.collider2D.enabled = true;
}
why doesn’t this work?
This is happening because you are disabling the player’s collision. The collision never can leave the trigger because he has no collider anymore. Basically, the player’s collision is going away completely when you set enabled to false. So there is no way for the collision to interact with the trigger anymore and you’re stick with it disabled.
To fix this, you’ll need another object attached to the player that is turning the player’s collision on and off. Attach a gameobject with a collider to the player. When this game object collides with the trigger, disable the player’s collision, then when this box leaves the trigger, enable the player’s collision. You will have to set this gameobject to not interact with any other collision aside from the trigger. You can do that with layers.