i want to press a button, than it plays an audio file, than it loads a other scene. here below i tried to script this. What is the reason why it is not working. What do i need to type to make it work. ------ var startup: AudioSource; function OnGUI(){ if (GUI.Button (Rect (23,180, 281, 70), "playmatch")) { audio.Play(startup); Application.LoadLevel("loading_new_game"); }
You don't need to create a variable to hold an AudioSource. GameObject.audio conveniently refers to the AudioSource currently attached to the game object. Calling audio.Play() will play the audio clip attached to the audio source. What you want to do: Attach an Audio Source component to the game object that the script is attached to Drag an audio clip from the Project view onto the "Audio Clip" variable in the inspector Call audio.Play();
The button is in a script attached to a game object. All scripts are components, and all components have to be attached to a game object. So whatever game object you attached your script to, also attach an Audio Source to it. Then you'll be able to access that Audio Source when the player clicks the button you have set up.
so, have the audio attached, an audi source,... but the script doens't make any noise. When I add yield new waitforsecond too it the button doesn't even show the button now... mmm function OnGUI(){ if (GUI.Button (Rect (105, 457, 100, 22), "MENU")) { audio.Play(); // yield new WaitForSeconds (2.0); Application.LoadLevel("intro"); } }