Problem with OnTriggerEnter2D

I have my player and it has a Box Collider 2D as “Is Trigger”.

I have a barrier where the player must jump to bypass. This barrier has a Box Collider 2D as “Is Trigger”.

When the player touches the barrier the following code is processed:

void OnTriggerEnter2D () {
         Sound.PlayOneShot (SoundHurt);
         Application.LoadLevel ( "gameover");
}

(So ​​far, everything is working perfectly)

I added a coin and it has a Box Collider 2D as “Is Trigger” and the goal is when the player touches it, the coin short screen. The following code is processed:

void OnTriggerEnter2D (Collider2D Coins) {
        Sound.PlayOneShot (SoundCoins);
        if (Coins.gameObject.tag == "Coins") {
                Destroy (Coins.gameObject);
        }
}

void OnTriggerEnter2D () {
         Sound.PlayOneShot (SoundHurt);
         Application.LoadLevel ( "gameover");
}

Now only the first OnTriggerEnter2D is working.

I guess I should not have put both (barrier and coin) as “Is Trigger”, but I can not think of another way and make both work correctly, as I wish.

Can anybody help me?

You can only have one “OnTriggerEnter2D” in your script. (I assume your script is on the player gameobject)

The cleanest way to do what you want is first add a tag to your barrier “Barrier” and change your code like this:

 void OnTriggerEnter2D (Collider2D other) {

         if (other.gameObject.tag == "Coins") {
                 Sound.PlayOneShot (SoundCoins);
                 Destroy (other.gameObject);
         }
         if (other.gameObject.tag == "Barrier") {
                 Sound.PlayOneShot (SoundHurt);
                 Application.LoadLevel ( "gameover");
         }
 }