Trigger sound when pressing button

I just want to be able to go up to my object and press a button which will make the sound I have imported and attached to the object play.
I’ve looked through a lot of scripting that is similar to what I want, but I have no idea how to change it to what I want.
Really appreciate some help. Thanks :slight_smile:

Here’s how I usually do audio

C#:

public AudioSource audio; //Create an audiosource component on the button and drag it here
public AudioClip buttonSound; //Put sound clip here

public bool inRange;

//I don't know how you want to trigger the sound, but here's a basic way
void Update(){

    if(Input.GetButtonDown("Use") && inRange){
        audio.PlayOneShot(buttonSound);
    }

}

Then have the bool set to true and false with a trigger
on the button. Since you don’t want to have to have 1000’s of audio
source component variables for a large scene you should use
GetComponent(>AudioSource>); with OnTriggerEnter.

@JoBrindley:

Here is an easy way to do what you want:

  1. Add an AudioSource (add component - AudioSource) to your button;
  2. Add the AudioClip you want to play to the AudioClip field in the AudioSource;
  3. Click the “+” at “On Click()” in the editor;
  4. Add the button to the field that reads “none (object)”;
  5. Click in the combo that reads “No Function” and then AudioSource>Play();

Ready. Test your button and it should play the audioclip when clicked. Cheers!