- Home /
Audio wont play when conditions are met
heres my core i have it set on an empty object called game over with a voice saying "game over" attached to my audio source
public class gameOver : MonoBehaviour { public GUISkin theskin;
Transform theBall;
void Start(){
theBall = GameObject.FindGameObjectWithTag ("ball").transform;
}
void Update(){
if (ScoreKeeper.Score == 10) {
audio.Play ();
}
else if (ScoreKeeper.Score2 == 10) {
audio.Play();}
}
void OnGUI(){
GUI.skin = theskin;
if (ScoreKeeper.Score==10){
GUI.Label(new Rect(Screen.width/2-350,Screen.height/2-140,2000,1000),"PLAYER 1 WINS!!");
theBall.gameObject.SendMessage("hasWon",null,SendMessageOptions.RequireReceiver);
if(GUI.Button(new Rect(Screen.width/2-100,Screen.height/2-50,200,75),"REMATCH")){
ScoreKeeper.Score=0;
ScoreKeeper.Score2=0;
theBall.gameObject.SendMessage("resetBall",.05f,SendMessageOptions.RequireReceiver);
}
if(GUI.Button(new Rect(Screen.width/2-100,Screen.height/2+50,200,75),"QUIT")){
ScoreKeeper.Score=0;
ScoreKeeper.Score2=0;
theBall.gameObject.SendMessage("resetBall",.05f,SendMessageOptions.RequireReceiver);
Application.LoadLevel(0);
}
}
else if(ScoreKeeper.Score2==10){
GUI.Label(new Rect(Screen.width/2-02.6f,Screen.height/2-140,2000,1000),"PLAYER 2 WINS!!");
theBall.gameObject.SendMessage("hasWon",null,SendMessageOptions.RequireReceiver);
if(GUI.Button(new Rect(Screen.width/2-100,Screen.height/2-50,200,75),"REMATCH")){
ScoreKeeper.Score=0;
ScoreKeeper.Score2=0;
theBall.gameObject.SendMessage("resetBall",.05f,SendMessageOptions.RequireReceiver);
}
if(GUI.Button(new Rect(Screen.width/2-100,Screen.height/2+50,200,75),"QUIT")){
ScoreKeeper.Score=0;
ScoreKeeper.Score2=0;
theBall.gameObject.SendMessage("resetBall",.05f,SendMessageOptions.RequireReceiver);
Application.LoadLevel(0);
}
} } } the problem is that the game over sound doesnt play unitl i press the rematch button instead of when the score equals 10 can you let me know whats wrong please?
Answer by robertbu · Aug 30, 2014 at 03:51 PM
You are calling 'audio.play()' every frame when the conditions are met. Try:
void Update(){
if (ScoreKeeper.Score == 10 && !audio.isPlaying) {
audio.Play ();
}
else if (ScoreKeeper.Score2 == 10 && !audio.isPlaying) {
audio.Play();}
}
thank you i think i did everything but that lol it was driving me crazy but now i've run into another problem i keeps repeating until i hit a button how would i go about making it play just once?
I'm not sure of what conditions you want it to play, but you can do it this way to play it exactly once:
private bool hasPlayed = false;
void Update(){
if (!hasPlayed && Score$$anonymous$$eeper.Score == 10) {
hasPlayed = true;
audio.Play ();
}
else if (!hasPlayed && Score$$anonymous$$eeper.Score2 == 10) {
hasPlayed = true;
audio.Play();}
}
Your answer
Follow this Question
Related Questions
Background Music 2 Answers
Play An Audio Clip When An Instantiated Object Collides 1 Answer
gameObject to play sound on deletion, won't work... 1 Answer
Make door play sound when you open and close it ? 1 Answer
Some audio does not play on Windows build but works perfectly in editor and on Mac 1 Answer