I am having troubles playing audio files during specific events.
First, I want the game to play an audioclip once the player is dead. This is the script (attached to an empty GameObject):
`
private PlayerHealth player;
bool dead = false;
AudioClip myaudio;
void Start () {
player = GameObject.FindWithTag("Player").GetComponent<PlayerHealth>();
}
void Update (){
dead = player.playerDead;
if (dead){
PlayStuff();
}
}
void PlayStuff () {
print ("Player is dead, play audio.");
audio.Play();
}`
I have tested this and the function runs when it should, but nothing plays. The aforementioned game object to which this script is attached has an Audio Source component with the desired audio clip.
Another goal is for an audio clip to be played by my enemies when they are in a certain radius of the player. I have not tried to accomplish this task as I expect I would run into the same problem.
It would be much appreciated if anyone could shed some light on this topic and give me a hand.