Collision issue - one way collision detection only

Hi peops. Firstly, I’m not quite sure which area to post this in, but as it only seems to happen on an orthographic camera I thought I’d add it in 2D.

Anyway, the issue. I have two virtually identical scripts on two objects which are sprites as follows…

public class PlayerCollisionChecker : MonoBehaviour {

    public Transform explosionEffect;

    private Transform player;

    void Start() {
        player = transform;
    }

    void OnCollisionEnter2D(Collision2D collision) {
        Debug.Log("Player collision with " + collision.gameObject.name);
        switch(collision.gameObject.name) {
            case "enemy0":
                Instantiate(explosionEffect, player.position, Quaternion.identity);
                player.GetComponent<PlayerControl>().resetPlayerPosition();
                break;
        }
    }
}

and

public class EnemyCollisionChecker : MonoBehaviour {

    public Transform explosionEffect;

    private Transform enemy;

    void Start() {
        enemy = transform;
    }

    void OnCollisionEnter2D(Collision2D collision) {
        Debug.Log("Enemy collision with " + collision.gameObject.name);
        switch(collision.gameObject.name) {
            case "Player Spaceship":
                Instantiate(explosionEffect, enemy.position, Quaternion.identity);
                enemy.GetComponent<EnemyAI>().resetEnemy();
                break;
        }
    }
}

However, only one of the objects is detecting a collision, and it’s not always the same one but I never get both collision detection at the same time.

Both my sprites have a Rigidbody2D and BoxCollider2D set on them and the fact that one or the other detects collision but not both at the same time obviously means that it’s working.

Should I be adding a delay before resetting the player/enemy? All that the reset function does is to simply relocate the sprite back to its original position (and hence, removes the collision but the original one should have already been detected).

Any help greatly appreciated as this is currently confusing me big time.

Chris

In 4.5 (unlike 4.3) things that happen in a collision event can prevent further calls from happening in that collision. So, yes, delaying the changes into Update would fix your problem (and is what I’m doing since moving from 4.3 to 4.5).

According to the release notes for the latest patch, I believe there’s a direct fix for your problem in it. There’s a setting to make collision events get called even if the collision is tampered with.

Thanks for the reply and info Pyrian. What I’ve done currently is to switch back to using 3D models and the 3D Rigidbody and a mesh collider. It’s a bit of overkill for a 2D application, but at least it’s working.