如何播放毁灭声音

时间:2015-01-02 05:25:13

标签: c# triggers unity3d gameobject

我正在进行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);
       }
}

1 个答案:

答案 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);
}