【发布时间】:2017-08-24 01:19:06
【问题描述】:
我希望我的游戏在玩家与硬币碰撞时播放声音,但没有播放声音。我没有收到任何错误。我在 Unity 中将声音附加到音频源和脚本。
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class TriggerCoin : MonoBehaviour
{
//TODO
//Counter of collected coins
public AudioClip coinSound;
new AudioSource audio;
void Start()
{
audio = GetComponent<AudioSource>();
}
void Update()
{
}
void OnTriggerEnter2D(Collider2D other)
{
GameObject gObj = other.gameObject;
if (gObj.CompareTag("Player"))
{
Debug.Log("You picked up coin!");
audio.PlayOneShot(coinSound, 0.8f);
Destroy(gameObject);
}
}
}
【问题讨论】:
-
认为这可能是因为您在播放剪辑更改之前破坏了游戏对象。尝试延迟破坏,
Destroy(gameObject, 1f);