- Home /
Audio track based on integer value. (integer value changed by GUI.Button)
I want to have three buttons that each when selected change the current audio track to track one, track two, and track three respectively. To do this, I defined a var for each of the audio sources along with defining an integer called SelectedTrack in order to make it so by clicking on a button the integer would be set to a value of 1,2, or 3 and that if SelectedTrack=1 then track one would play and so on for track two and three.
This is the code that I have right now for my GUI setup:
var PauseWallpaper : GUISkin; var TrackOne : AudioSource; var TrackTwo : AudioSource; var TrackThree : AudioSource; var SelectedTrack : Int = 1; static var ToggleMenu : boolean = false; function Update() { if(Input.GetKeyDown(KeyCode.Escape)){ GetComponent("MouseLook").enabled = !GetComponent("MouseLook").enabled; GetComponent("Camera").enabled = !GetComponent("Camera").enabled; PauseGame(); } if(SelectedTrack=1){ audio.Play(TrackOne); } if(SelctedTrack=2){ audio.Play(TrackTwo); } if(SelctedTrack=3){ audio.Play(TrackThree); } }
static function PauseGame() { if(!ToggleMenu) { Time.timeScale=0; AudioListener.pause = true; ToggleMenu=true; } else { Time.timeScale=1; AudioListener.pause = false; ToggleMenu=false; } }
function OnGUI() { if(GUI.Button(Rect(0,0,100,50),"Track One")){ SelectedTrack = 1; } if(GUI.Button(Rect(0,0,100,50),"Track Two")){ SelectedTrack = 2; } if(GUI.Button(Rect(0,0,100,50),"Track Three")){ SelectedTrack = 3; } if(ToggleMenu) //if ToggleMenu = true execute follwing. { GUI.skin = PauseWallpaper; GUI.Box(Rect (0,0,Screen.width,Screen.height), "PAUSED"); if(GUI.Button(Rect (Screen.width / 2 - 50, Screen.height / 2 - 75, 100, 50), "Exit Program")){ Application.Quit(); } if(GUI.Button(Rect (Screen.width / 2 - 50, Screen.height / 2 - 25, 100, 50), "Level Select")){ //script } } }
These are the errors that currently come up : (14,25): BCE0044: expecting ), found '='. (14,26): BCE0043: Unexpected token: 1. (15,29): BCE0044: expecting :, found ';'.
Lines 43 to 51 is where I want to change the integer value when each button is pressed. and 14 to 22 is where I want the current track playing to be based off of the integer value.
Any help figuring this out is greatly appreciated.
Answer by Hei · May 05, 2011 at 10:39 AM
if you use if(...), you should use the == , example - if (SelectedTrack == 1){ your script in here ... }
And play other audio , you should use the AudioChip and audio.PlayOneShot,
example - var TrackOne : AudioChip;
audio.PlayOneShot(TrackOne);
var PauseWallpaper : GUISkin;
// Put your audio clip in the TrackOne, TrackTwo and TrackThree, but not the audio source
var TrackOne : AudioClip;
var TrackTwo : AudioClip;
var TrackThree : AudioClip;
var SelectedTrack : Int = 1;
static var ToggleMenu : boolean = false;
function Update() { if(Input.GetKeyDown(KeyCode.Escape)) { GetComponent("MouseLook").enabled = !GetComponent("MouseLook").enabled; GetComponent("Camera").enabled = !GetComponent("Camera").enabled; PauseGame(); }
if(SelectedTrack == 1)
{
audio.PlayOneShot(TrackOne);
}
if(SelectedTrack == 2)
{
audio.PlayOneShot(TrackTwo);
}
if(SelectedTrack == 3)
{
audio.PlayOneShot(TrackThree);
}
}
static function PauseGame() { if(!ToggleMenu) { Time.timeScale=0; AudioListener.pause = true; ToggleMenu=true; } else { Time.timeScale=1; AudioListener.pause = false; ToggleMenu=false; } }
function OnGUI() { if(GUI.Button(Rect(0,0,100,50),"Track One")) { SelectedTrack = 1; }
if(GUI.Button(Rect(0,0,100,50),"Track Two"))
{
SelectedTrack = 2;
}
if(GUI.Button(Rect(0,0,100,50),"Track Three"))
{
SelectedTrack = 3;
}
if(ToggleMenu) //if ToggleMenu = true execute follwing.
{
GUI.skin = PauseWallpaper;
GUI.Box(Rect (0,0,Screen.width,Screen.height), "PAUSED");
if(GUI.Button(Rect (Screen.width / 2 - 50, Screen.height / 2 - 75, 100, 50), "Exit Program"))
Application.Quit();
if(GUI.Button(Rect (Screen.width / 2 - 50, Screen.height / 2 - 25, 100, 50), "Level Select"))
{
//script
}
}
}
There are some script reference - AudioClip and PlayOneShot
Thanks, I ended up finding a workaround before I saw this, but it helps out to know about the other stuff as well for future reference.
Your answer
Follow this Question
Related Questions
Singleton Error with code 1 Answer
not sure whats happened :S 1 Answer
SwipeControl package error in unity iPhone 1.7 1 Answer
Please help, syntax error woe 2 Answers
Javascript Expecting Errors ';' & '=' 0 Answers