Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Apples_mmmmmmmm · May 05, 2011 at 01:14 AM · bce0044bce0043

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Apples_mmmmmmmm · May 05, 2011 at 06:18 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges