1

我正在制作一个 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);
       }
}
4

1 回答 1

1

您需要延迟破坏,以便您可以实际播放声音,因为它们发生在同一个对象中。

void OnTriggerEnter2D(Collider2D other)
{
    if(other.tag == "Player")
    {
        coin++; 
        StartCoroutine(Ding());  
    }
}

IEnumerator Ding()
{
    audio.PlayOneShot(coinSound);
    yield return new WaitForSeconds(5);
    Destroy(gameObject);
}
于 2015-01-02T06:28:45.160 回答