- Home /
Play sound on collision doesn't work
This is my code (c#)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class collidersound : MonoBehaviour {
public AudioSource oofSource;
// Use this for initialization
void Start () {
oofSource = GetComponent<AudioSource> ();
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter (Collision collision) {
if (collision.gameObject.tag == "Floor") {
oofSource.Play ();
}
}
}
This script throws up no errors. The problem is that nothing works. No sound is playing when it collides.
It's really odd.
Thanks for any help in advance.
Are you sure the collision is detected ? (put Debug.Log
to check inside the method, outside the condition and inside the condition).
If you have a repository of the project you can give me that and I can check it out to try
Answer by Reynarz · Aug 15, 2017 at 01:45 PM
If you set the AudioSource component instance through the editor don't try to get it again. Delete the entire Start(); method
Okay, just removed the Start(); section.
Still no sound received.
Did you set the AudioSource from the editor? Show me your configuration of the editor, and how you set the floor tags, and all that stuff. Your question is not completed :/
Yeah, I never thought the editor would have anything to do with it.
The tag was used as a match to the hierarchy "Floor" is that incorrect use?
Sorry for not being clear.
Your answer
Follow this Question
Related Questions
Sound plays right at the beginning 2 Answers
Colliding two GameObjects 1 Answer
Jump on the ground 2 Answers
OnTriggerExit stop audio 1 Answer
How do i destroy only 1 object when 2 instances of the same object collide ? 2 Answers