简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM