简体   繁体   English

碰撞中的Unity2D AudioSource

[英]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. 问题是,您在播放音频后立即销毁正在播放音频的GameObject。 Consider attaching the AudioSource to a other GameObject instead. 考虑将AudioSource附加到另一个GameObject。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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