I am trying to play a short sound when the player enters a trigger/walks into a coin-object.
I imported the sound I want, and tried different things, but nothing worked so far.
I tried making a javascript
var soundFile:AudioClip;
function OnTriggerEnter(trigger:Collider) {
if(trigger.collider.tag=="Player") {
audio.clip = soundFile;
audio.Play();
}
}
added the sound in the inspector, though it doesn’t play at all.
I tried adding it to my C#-script I allready have for the coin and created an audio-source on the object, but it doesn’t play either:
[RequireComponent(typeof(AudioSource))]
public class Coin : MonoBehaviour {
private AudioClip gong;
void OnTriggerEnter(Collider c)
{
if (c.gameObject.tag == "Player")
{
audio.PlayOneShot(gong, 1.0f);
//audio.Play();
Destroy (gameObject);
}
}
}
I tried both audio.PlayOneShot, as well as audio.Play (not sure how many others there are), no sound plays at all. Is there something I need to check in the Audio-Source? I unchecked “Play On Awake” since I want the sound only when entering the Trigger. With Play on Awake, it played when loading the Scene at the beginning at least.