- Home /
Problem with playing sound on Trigger-Enter
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.
Is this a 2D sound? If it's a 3D sound, is the AudioListener close enough to the source?
it's a 3D Sound and the player, which I think should be the AudioListener, is exactly on the source at the time of collision... By now I have advanced with the problem and started a new question since it's a different problem: new question
Answer by legion_44 · Jun 12, 2014 at 05:54 PM
Are You calling Destroy on object with AudioSource that's playing the sound?
audio.PlayOneShot(gong, 1.0f); //Here audio starts being player
Destroy(gameObject); //And here ends, possibly even in the same frame
If You wan't to destroy this gameobject and not need settings audio effects in inspector try
AudioClip clip;
void OnTriggerEnter(Collider c)
{
AudioSource.PlayClipAtPoint(clip, gameObject.position);
Destroy(gameObject);
}
That for some reason didn't seem to work... I had to change the gameObject.position to gameObject.transform.position for the code to accept it... though now, I get a lot of "NullReferenceExceptions", and when I walk into the object, it doesn't destroy, just cause some lag and adds a gameObject called "One Shot Audio" to the scene every time.
I figured out more now, as I added as comment above, I advanced to a new problem with it. That can be found here
Answer by GuntranHoet · Aug 16, 2015 at 09:38 AM
For your C# script:
[RequireComponent(typeof(AudioSource))]
public class Coin : MonoBehaviour {
private AudioSource Source;
public AudioClip gong;
void Start()
{
Source = GetComponent<AudioSource>();
}
void OnTriggerEnter(Collider c)
{
if (c.gameObject.tag == "Player")
{
//audio.PlayOneShot(gong, 1.0f);
//audio.Play();
Source.Clip = gong;
Source.Play();
Destroy (gameObject);
}
}
}
Doesn't work, Source.Clip returns this: 'AudioSource' does not contain a definition for "clip' and no extension method 'Clip' accepting a first argument of type 'AudioSource' could be found.
Your answer
Follow this Question
Related Questions
2nd Audio Clip Not Playing 0 Answers
How Do I Get This To Play Sound? 0 Answers
OnTriggerExit stop audio 1 Answer
How to make audio not playing repeatedly? 1 Answer
How do I play audio source on Maximize on Play / Audio Source wont play on Maximize 0 Answers