Antoine Olivier
Antoine Olivier

Reputation: 83

Unity2D AudioSource on Collision

I attached an audiosource to a gameobject and in a script attached to the played I put :

 void OnCollisionEnter2D (Collision2D col) {
         if (col.gameObject.tag == "tag") {
             col.gameObject.GetComponent<AudioSource>().Play();
             Destroy(col.gameObject);
             GetComponent<Score>().score += 1;
         }
     }

In order to play a sound when there is a collision. But it's not working and I wonder where is the mistake. Thanks for the help. Everything but the sound is working it's not a collision nor a volume problem.

Upvotes: 0

Views: 64

Answers (1)

Ian H.
Ian H.

Reputation: 3919

The problem is that you are destroying the GameObject that is playing the audio immediatly after you play it. Consider attaching the AudioSource to a other GameObject instead.

Upvotes: 0

Related Questions