- Home /
audio.Play(); not working while audio.Stop(); works fine when the audio is played on awake
In my game we have this dialogue sequence where you have some dialogue text and corresponding audio, and then you press space to hear/read the next line. I have this strange problem where I can't get audio.Play(); to work. There's a strange static sound I get for a second when the script is initialized and it goes through the audio.Play(); as if it is trying to play the audio but it isn't working.
var dialogueStart : boolean = false;
private var dialogue : int = 1;
var dialogueBox : GUIStyle = new GUIStyle();
var d1 : String;
var d2 : String;
var d3 : String;
private var a1 : AudioSource;
private var a2 : AudioSource;
private var a3 : AudioSource;
function Start(){
var aSources = GetComponents(AudioSource);
a1 = aSources[0];
a2 = aSources[1];
a3 = aSources[2];
}
function Update () {
if (Input.GetKeyDown("space")){
dialogue++;
}
}
function OnGUI(){
if (dialogueStart == true){
Time.timeScale = 0;
switch (dialogue)
{
case 1 :
GUI.Box(Rect(0,Screen.height*.8,Screen.width,Screen.height*.2),d1, dialogueBox);
a1.Play();
break;
case 2 :
GUI.Box(Rect(0,Screen.height*.8,Screen.width,Screen.height*.2),d2, dialogueBox);
a1.Stop();
a2.Play();
break;
case 3 :
GUI.Box(Rect(0,Screen.height*.8,Screen.width,Screen.height*.2),d3, dialogueBox);
a2.Stop();
a3.Play();
break;
case 4 :
a3.Stop();
break;
}
}
}
If I set the audio to play on awake, the audio.Stop(); works. I have seen someone else on the site have a similar problem. So, I tried the play on awake, and changing the volume method and that works, however by the time the audio is played it is in the middle of the audio and apparently you can't tell Unity to play an audio clip at a specific time so I don't think this method will work.
Any help or tips are much appreciated! (:
@Oakie might try checking if audio is playing, and if it is, then stop the previous audio, and then start the new audio, not sure if that will help or not.
Good idea, I'll give it a go... Okay unfortunately didn't work, but I decided to throw some Debug.logs inside the .isPlaying if statements, and it made me realize that every single frame it is probably trying to call the audio, hence it not being able to actually play it. Okay I think I know what to do now, I'll update once I see if this works or not
Hmm, yeah gotta make sure to only to play or stop one time, ensuring you dont stop the next audio clip and such. Good luck!
Ohh also, if you dont set it to play on awake, then you script in something that happens after an amount of time or whatever, then Play() the audio, it should work that way. Another thing is, I would have a var for each audio clip, and then set them as public, and in the inspector assign each part of the audio, and Play() each one as you change the text on screen...rather than the way you just have audio[1] and such...
Thank you for the help! It would have been very bad if I couldn't figure that out in the next few hours, so you really helped me out of this bind (:
Answer by Oakie · Jun 03, 2012 at 06:52 AM
Not sure of the protocol on this website concerning when a person is able to answer their own question, but I needed to make sure that the audio was only called upon once and not once per frame. So this was the end code that works, for anyone curious:
var dialogueStart : boolean = false;
private var dialogue : int = 1;
var dialogueBox : GUIStyle = new GUIStyle();
var d1 : String;
var d2 : String;
var d3 : String;
private var a1 : AudioSource;
private var a2 : AudioSource;
private var a3 : AudioSource;
var frameOnce : boolean = true;
function Start(){
var aSources = GetComponents(AudioSource);
a1 = aSources[0];
a2 = aSources[1];
a3 = aSources[2];
frameOnce = true;
}
function Update () {
if (Input.GetKeyDown("space")){
dialogue++;
frameOnce = true;
}
}
function OnGUI(){
if (dialogueStart == true){
Time.timeScale = 0;
switch (dialogue)
{
case 1 :
GUI.Box(Rect(0,Screen.height*.8,Screen.width,Screen.height*.2),d1, dialogueBox);
if (frameOnce == true){
a1.Play();
frameOnce = false;
}
break;
case 2 :
GUI.Box(Rect(0,Screen.height*.8,Screen.width,Screen.height*.2),d2, dialogueBox);
if (frameOnce == true){
a1.Stop();
a2.Play();
frameOnce = false;
}
break;
case 3 :
GUI.Box(Rect(0,Screen.height*.8,Screen.width,Screen.height*.2),d3, dialogueBox);
if (frameOnce == true){
a2.Stop();
a3.Play();
frameOnce = false;
}
break;
case 4 :
if (frameOnce == true){
a3.Stop();
}
break;
}
}
}
Your answer
Follow this Question
Related Questions
Can an object check on other objects? 1 Answer
master background music toggle not working after changing scenes 1 Answer
AudioSource.clip.time won't work? 2 Answers
Audiosource.Play() not working 0 Answers
Weird audio problem 1 Answer