Search Unity

button making sound

Discussion in 'Scripting' started by lesfundi, Aug 18, 2009.

  1. lesfundi

    lesfundi

    Joined:
    Jan 10, 2009
    Posts:
    628
    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");
    }
     
  2. matthewminer

    matthewminer

    Joined:
    Aug 29, 2005
    Posts:
    331
    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:
    1. Attach an Audio Source component to the game object that the script is attached to
    2. Drag an audio clip from the Project view onto the "Audio Clip" variable in the inspector
    3. Call audio.Play();
     
  3. lesfundi

    lesfundi

    Joined:
    Jan 10, 2009
    Posts:
    628
    but it is a button, not an object.
     
  4. matthewminer

    matthewminer

    Joined:
    Aug 29, 2005
    Posts:
    331
    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.
     
  5. lesfundi

    lesfundi

    Joined:
    Jan 10, 2009
    Posts:
    628
    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");

    }






    }
     
  6. lesfundi

    lesfundi

    Joined:
    Jan 10, 2009
    Posts:
    628