audio.Play() not working?

Alright, so I have the following script called Timer.cs.

Using UnityEngine;
using System.Collections;

public class Timer : MonoBehaviour {
	

	public float timer = 300; // set duration time in seconds in the Inspector
	public static int sound = 1;
	public static int go = 1;
	bool  isFinishedLevel = false; // while this is false, timer counts down

	void  Start(){
		PlayerController.speed = 8;
		PlayerController.jumpHeight = 12;
	}
	
	void  Update (){
		if (!isFinishedLevel) // has the level been completed
		{
			timer -= Time.deltaTime; // I need timer which from a particular time goes to zero
		} 
		
		if (timer > 0)
		{
			guiText.text = timer.ToString();
		} 
		else 
		{
			guiText.text = "GAME OVER!"; // when it goes to the end-0,game ends (shows  time text   over...) 

			audio.Play();

			int getspeed = PlayerController.speed;
			PlayerController.speed = 0;

			int getjumpHeight =  PlayerController.jumpHeight;
			PlayerController.jumpHeight = 0;
		}
		if (Input.GetKeyDown("r")) // And then i can restart game: pressing restart.
		{ 
			Application.LoadLevel(Application.loadedLevel); // reload the same level
		}
	}
}

This script is connected to some GUI Text, which displays the amount of time remaining in the game. Also attached to this script is an Audio Source with my desired sound selected. When the clock reaches zero, the text changes to say “GAME OVER!” and the character controls lock up; however, the sound does not play. All other instances of audio.Play() in my scene are working fine, and when I set the Audio Source to “Play On Awake”, it plays without a problem. What could be the problem?

Thanks in advance.

Try changing audio.Play() into AudioSource.PlayClipAtPoint(clip,transform.position) and tell me the result. And remember to assign an AudioClip variable prior to that. Here’s a link on it if you need it: http://docs.unity3d.com/Documentation/ScriptReference/AudioSource.PlayClipAtPoint.html

This method has never failed me, so I hope it won’t fail you either.

This is what I use: Debug.Log("Hey2"); GetComponent<AudioSource>().Play();

Pretty late, but i had the same problem today and the solution is, not to start play in short loops like update.
Check if audio isplaying is false, otherwise you start the clip, every time you call play, from the beginning and you do not hear anything.

In my case, the sound plays on awake, but then don’t work when y call AudioSource.Play()

My problem was that I run out of memory or something like that. All because a use too much VideoPlayer.Prepare

Check if you are loading too much resources.

Hi @miketyler1
the problem is that your Update method restart every frame, so when the timer reach 0 for the first time, it remains to 0 for each other frame… in this way, every frame execute this code:

 guiText.text = "GAME OVER!"; // when it goes to the end-0,game ends (shows  time text   over...)
audio.Play();

int getspeed = PlayerController.speed;
PlayerController.speed = 0;

int getjumpHeight =  PlayerController.jumpHeight;
PlayerController.jumpHeight = 0;

So, your AudioController will restart playing the sound continuously, making it inaudible.
To resolve this, add this code at the start of Update method:

if(timer <= 0)
    return;

or avoid with other methods to execute continuously that piece of code.