- Home /
Duplicate Question : http://answers.unity3d.com/questions/661520/play-sound-on-destroy.html
Play Sound on Destroy
So I have looked around the Internets and I haven't been able to find a decent explaination for my question. Basically I want a short clip to play after my character "picks up" a coin (more like destroys it). I've tried several times using different techniques so I just wanted to ask myself to see if anyone can tell me how to make this work. Here's the code, and if you need any clarification I'd be happy to help.
using UnityEngine;
using System.Collections;
public class PowerUpScript : MonoBehaviour {
HUDScript hud;
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Player")
{
hud = GameObject.Find ("Main Camera").GetComponent<HUDScript>();
hud.IncreaseScore(10);
Destroy (this.gameObject);
}
}
}
Answer by JoeyP · Mar 11, 2014 at 09:58 PM
Hi, I am a new developer too so I apologise in advance if my solution does not work :)
I think this may work
using UnityEngine;
using System.Collections;
[RequireComponent] (typeof (AudioSource))]
public class PowerUpScript : MonoBehaviour {
HUDScript hud;
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Player")
{
hud = GameObject.Find ("Main Camera").GetComponent<HUDScript>();
hud.IncreaseScore(10);
Destroy (this.gameObject);
audio.Play();
}
}
}
Then go back into Unity and then click on the object with the script(the coin i think) and add Component>Audio>Audio Source from the top of Unity, untick "Play on awake" Drag the audio clip into the "Audio Clip" on the right. Then test it?
I got it to work by using an AudioClip. Thanks anyway Joey!
Follow this Question
Related Questions
Play Sound on Destroy 1 Answer
How to do 2d audio properly? 1 Answer
Attach audio source to different part of object 0 Answers
Make Sound Effects Not Audible Unless Near Them 2d? 1 Answer