- Home /
Can someone please tell me why the sound in this script would not play? I don't get any errors just the sound clip does'nt work.
var boom8 : AudioClip;
function Start () {
}
function Update () {
transform.position.x += Input.GetAxis("Horizontal") * playerSpeed * Time.deltaTime;
if (transform.position.x > maxWidth)
transform.position.x = maxWidth;
if (transform.position.x < -maxWidth)
transform.position.x = -maxWidth;
if (Input.GetButtonDown("Jump"))
Instantiate(bullet, bulletSpawn.position, bulletSpawn.rotation);
if (Input.GetButtonDown("Quit"))
Application.Quit();
}
function OnTriggerEnter (other : Collider)
{
if (other.tag == "Enemy")
{
audio.PlayOneShot(boom8,0.9);
Instantiate (explosion, transform.position, transform.rotation);
Instantiate (Restarter, transform.position, transform.rotation);
Destroy (gameObject);
}
}
You need an Audio Source on this object(if you don't have one already), you also need to set the boom8 AudioClip to this source.
http://docs.unity3d.com/ScriptReference/AudioSource.html
http://docs.unity3d.com/ScriptReference/AudioSource-clip.html
But I think a missing audioSource gives a run-time error.
And PlayOneShot is funny -- it's for "other" clips not attached to the audioSource. I use the OPs set-up (even 0.9 for sound level, empty clip in the audioSource slot) and it's fine.
Now, if boom8 was empty, that won't give an error (you'd think it would, but I guess audio is used to getting nulls?)
Answer by ShdwWolfAlpha · Dec 19, 2014 at 07:56 AM
So it turns out the problem is that the Destroy gamObject command is destroying the ship before this sound can go off.
Your answer
![](https://koobas.hobune.stream/wayback/20220613174317im_/https://answers.unity.com/themes/thub/images/avi.jpg)