Problem triggering using colliders

Hi, I just started Unity a couple days ago. I am primarily an audio designer and am completely new to coding, so it’s been a bit of a challenge at times. I’ve been following tutorials to put together a 2D platformer.

I’ve been using this Sebastian Lague Youtube series to do the mechanics 2D Platformer Controller (Unity 5) - YouTube

I’ve also used this Unity audio tutorial to get my music set up in mixers and snapshots, since I want the music to change as you progress Recorded Video Sessions on Audio - Unity Learn

After the platforming mechanics were set-up, and after setting up all the mixers, I’ve had problems doing the two following things, which together make me think there’s a problem with how things are colliding:

  1. Making a collectable item/coin that would be destroyed when colliding with the player. The player pushes around the coin instead of destroying it. I added the print line to see if this script was running at all, and the text did not show up in the console when pushing around the coin.

void OnCollisionEnter2D (Collision2D col) { if (col.gameObject.tag == "Player") { Destroy (gameObject); print ("nomnom"); } }

  1. Using collision with a trigger zone to trigger transitions between mixers. The audio does not change when the player enters the designated zone. I’ve followed all the steps in the Unity tutorial here, changing 3D objects for 2D ones, and adding “2D” (i.e. Collider2D, etc.) to some of the code where necessary.

void OnTriggerEnter2D(Collider2D other) { if (other.CompareTag ("TargetZone1")) { bg2a.TransitionTo (m_TransitionIn); } }

My suspicion is that collision is now wonkier than usual since there was so much code dealing with colliders in the above mentioned Youtube tutorial. Since I’m not very familiar with what all the code did exactly… I can’t be sure.

Also, I’ve attached the audio script to the player, as per the tutorial, but am not completely sure where to attach the coin script - the player or the coin? ( Just to clarify, I tried it on both, and neither made a difference in making it function)

Sorry for so many questions - I knew absolutely nothing about coding before starting this 2 days ago.
Any ideas or suggestions are welcome! I can provide more info if needed. Thanks!

This is a simple problem and there is no “wonky” business going on. Triggers and collisions are pretty straight forward.

The mistake you are making is probably just your setup. I"ll post an example:

Player has a BoxCollider2D > Ridgidbody2D > tagged as “player”

**

Coin has BoxCollider2D > Checked as Trigger

Now just put this code into the coin class:

   private void OnTriggerEnter2D(Collider2D col) {
        if (col.gameObject.tag == "player") {
            Destroy(gameObject);
        }
    }

The Above is an example for a Trigger, if you want to setup a Collision example then:

  • Add a Ridgidbody2D to the coin
  • Uncheck “isTrigger” in the coin’s boxCollider2D

Add this code into the coin script

   private void OnCollisionEnter2D(Collision2D col) {
        if (col.gameObject.tag == "player") {
            Destroy(gameObject);
        }
    }