- Home /
Can't get my sound to work..
Hey everyone!
I am having a problem with playing my sounds. So basically my sound sometimes does play and it sometimes doesn't play.
What I did so far is create a bool that checks if the sound is played already and i've created another if statement to make sure audio.play is only in the code once. Previously I didn't have this if statement and just copied and paste the audio.play two times in my other 2 if statements and that didn't work either.
Any help would be much apreciated!
This is my code so far:
void OnGUI() {
GUI.skin = customSkin;
//Show current bacons.
GUI.Label (new Rect(camera.pixelWidth / 20, camera.pixelHeight / 40, camera.pixelWidth / 5, camera.pixelHeight / 7), "<size='" + (camera.pixelWidth / 20) + "'>" + PlayerPrefs.GetInt ("BaconTotal") + "</size>");
GUI.Box (new Rect(camera.pixelWidth / 5, - (camera.pixelHeight / 4), (camera.pixelWidth / 4 * 3), (camera.pixelHeight / 5 * 4)), "", customSkin.GetStyle("Logo"));
if(GUI.Button (new Rect(camera.pixelWidth / 5 * 2, (camera.pixelHeight / 5), (camera.pixelWidth / 5), (camera.pixelHeight / (625 / 100))), "", customSkin.GetStyle("Play"))) {
loadLevel = 1;
buttonPressed = true;
}
else if(GUI.Button (new Rect(camera.pixelWidth / 5 * 2, (camera.pixelHeight / 2) - (camera.pixelHeight / 10), (camera.pixelWidth / 5), (camera.pixelHeight / (625 / 100))), "", customSkin.GetStyle("Store"))) {
loadLevel = 2;
buttonPressed = true;
}
if(buttonPressed && PlayOnce == false) {
Application.LoadLevel(loadLevel);
audio.Play();
}
}
So a little update. Firstly I must state that the sound sometimes does play and sometimes doesn't and secondlyI posted the wrong code :P. Here is my code:
void OnGUI() { GUI.skin = customSkin;
//Show current bacons.
GUI.Label (new Rect(camera.pixelWidth / 20, camera.pixelHeight / 40, camera.pixelWidth / 5, camera.pixelHeight / 7), "<size='" + (camera.pixelWidth / 20) + "'>" + PlayerPrefs.GetInt ("BaconTotal") + "</size>");
GUI.Box (new Rect(camera.pixelWidth / 5, - (camera.pixelHeight / 4), (camera.pixelWidth / 4 * 3), (camera.pixelHeight / 5 * 4)), "", customSkin.GetStyle("Logo"));
if(GUI.Button (new Rect(camera.pixelWidth / 5 * 2, (camera.pixelHeight / 5), (camera.pixelWidth / 5), (camera.pixelHeight / (625 / 100))), "", customSkin.GetStyle("Play"))) {
loadLevel = 1;
buttonPressed = true;
}
else if(GUI.Button (new Rect(camera.pixelWidth / 5 * 2, (camera.pixelHeight / 2) - (camera.pixelHeight / 10), (camera.pixelWidth / 5), (camera.pixelHeight / (625 / 100))), "", customSkin.GetStyle("Store"))) {
loadLevel = 2;
buttonPressed = true;
}
if(buttonPressed && PlayOnce == false) {
PlayOnce = true;
audio.Play();
Application.LoadLevel(loadLevel);
}
}
The problem is that as soon the sound start it also load the level thats why it gets cut, before loading the level you have to wait for the sound to finish or it will be random depending on how much time it gets to load the next level.
Try to change Application.LoadLevel(loadLevel); to
if(!audio.isPlaying) Application.LoadLevel(loadLevel);
So in this way first plays the audio and when is done proceed to load the level.
Have you tried commenting out the load level on line 18. It may be playing and then immediately stopping when it loads the next level.
Answer by BabyDev · Jul 16, 2014 at 04:37 PM
Allright so I exported to an .Apk and on my android phone it works perfectly. Wierd that it doesn't work within Unity then.
Thanks anyways!
Your answer
Follow this Question
Related Questions
Audio files not playing at all 4 Answers
Audio Not Always Playing 3 Answers
Background Music 2 Answers
Audio wont play when conditions are met 1 Answer
Play An Audio Clip When An Instantiated Object Collides 1 Answer