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! (: