- Home /
Playing certain sound during collision
I been researching how to do this and it seems that it doesn't work in Unity 5. I have two audio attached to a gameobject. 'swoosh' and 'death'. I want to play death during collision and my code seems to not working.
AudioSource source
void Start () {
source = GetComponent<AudioSource> ();
}
void OnTriggerEnter2D(Collider2D collider){
if (collider.tag == "Player") {
source.PlayOneShot(swoosh);
}
}
I keep getting error `swoosh' does not exist in the current context. I've seen people doing this in tutorials but it seems to be not working on mine. I'm using Unity 5
Answer by DoTA_KAMIKADzE · Apr 13, 2015 at 10:13 PM
In order to use PlayOneShot function you need a reference to your swoosh AudioClip;
AudioClip swoosh;//same for your second one
And then get your swoosh reference either on Awake/Start like you do with your AudioSource or make it public/[SerializeField] and set it through inspector.
Your answer
Follow this Question
Related Questions
How to detect collision on only one side of an object? [C#] 4 Answers
How to change fall delay of a gameobject by time? 2 Answers
How to detect if an object is within another object??? (Hide and seek type game). Thanks! 2 Answers
OnCollisionEnter2D not being called on entering new tile once already called. 1 Answer