我正在进行2D游戏并且它有硬币,当玩家触摸硬币时,我试图让硬币消失并发出叮当声。问题是硬币消失但没有声音。
using UnityEngine;
using System.Collections;
public class coins : MonoBehaviour {
static int coin = 0;
AudioClip coinSound;
void Start()
{
coin = 0;
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
coin++;
audio.PlayOneShot(coinSound);
StartCoroutine(Ding());
Destroy(this.gameObject);
}
}
void OnDisable(){
PlayerPrefs.SetInt ("coin", coin);
}
IEnumerator Ding(){
yield return new WaitForSeconds (0.4F);
}
}
答案 0 :(得分:1)
你需要延迟破坏,以便你可以实际播放声音,因为它们发生在同一个物体中。
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Player")
{
coin++;
StartCoroutine(Ding());
}
}
IEnumerator Ding()
{
audio.PlayOneShot(coinSound);
yield return new WaitForSeconds(5);
Destroy(gameObject);
}