Sound is not playing.

Hi all,

I hope I will be clear enough about my problem.
I am creating very simple app or game(on android) where u just type the word and the object connected to that word will disappear(fade animation) and the sound will be played.

How it should work: In the app I touch the screen the native keyboard come up. I type the word the object connected to that word will disappear and the sound clip has been played along with the animation.

Reality : I touch the screen the keyboard come up I type the word, I hit done, the native keyboard disappear, the object disappear animation is played correctly but there is no sound. When I touch screen again the native keyboard come up and the sound clip is played and I dont know why.

I created a empty gameobject and put sound clip to it.Thats how I am trying to play the sound.

Here is the code :
#pragma strict

function Start () {

}
var putString:String="";
var inputString:String="";
var keyboard:TouchScreenKeyboard;
var inv=new Array("laptop","mug","table");


function wordLoop(){

for( var value : String in inv){

if(inputString==value){

GameObject.Find(value).animation.Play("fade");
//Destroy(GameObject.Find(value),1);
GameObject.Find("sound").audio.Play();
//yield WaitForSeconds(1);



}
}
}
function Update(){


if(Input.touchCount>0){

keyboard=TouchScreenKeyboard.Open(putString);
}
if(keyboard.done){

inputString=keyboard.text;
wordLoop();

}
   

}

Can you please help me where is the problem that the sound is not played along with the animation but after the keyboard comes up?

Thanks in forward.

Martin.

//Use a script to manage your sound

public class SoundManager : MonoBehaviour {

private AudioClip backgroundMusic;
public AudioClip BackgroundMusic{
	set{
		backgroundMusic=value;
	}
	get{
		return backgroundMusic;
	}
}

 void Awake(){
	
	this.BackgroundMusic = Resources.Load("Sounds/background_music", typeof(AudioClip))as AudioClip;
	if(!this.BackgroundMusic){
		Debug.LogError(" background_music not found or name shoud be background_music");
	}
	
}

public void PlayBackgroundMusic(){
		audio.clip=this.BackgroundMusic;
		audio.volume=Constants.volBackgroundMusic;
		audio.Play();
		audio.loop=true;
}

public void StopBackgroundMusic(){
		if(audio.isPlaying){
			audio.Stop();
		}
}

}

The problem is that you’re trying to play the Audio once every frame. In the update() or the wordLoop() check if audio is playing first.

Suggestion: Put the GameObject.Find in the Start() because you don’t need to do this every frame either.