Audio won't play on collision
EDIT: I figured out what I was doing wrong. I was also making the collided GameObject inactive in the same frame audio was supposed to play. This kept audio from playing.
I'm becoming very frustrated trying to get an AudioClip to play when my character collides with an object. I have an AudioSource with the clip added to it attached to the GameObject to be collided with. This is what the code looks like:
using UnityEngine;
using System.Collections;
public class ObjectInteractionScript : MonoBehaviour {
private AudioSource source;
void Awake () {
source = gameObject.GetComponent<AudioSource>();
}
void OnCollisionEnter(Collision col) {
if (col.gameObject == GameObject.Find ("Ella")) { // Make sure the collision is with the character
print ("Please play the sound!!!");
source.Play();
}
}
}
This script is attached to the GameObject I'm colliding with. The print statement prints on collision and there are no errors, so I don't know why the sound isn't playing. I tried clicking Play On Awake in the AudioSource and the clip played when I ran the game, but it won't play on collision. What am I missing here?
Your answer
Follow this Question
Related Questions
Audio playing at hightspeed and simoultanesly various times! 1 Answer
I want the audio to play when the player nears the object, but fade away when they walk away. 0 Answers
Audio Cutting Out Unexplainably 1 Answer
how to make the a UI slider to control Music 1 Answer
All my spectrums from GetSpectrumData are the same 0 Answers