- Home /
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 :)
Answer by Josh707 · Jan 08, 2013 at 04:08 AM
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.
Answer by NicoSaraintaris · Oct 18, 2015 at 09:19 AM
Here is an easy way to do what you want:
Add an AudioSource (add component - AudioSource) to your button;
Add the AudioClip you want to play to the AudioClip field in the AudioSource;
Click the "+" at "On Click()" in the editor;
Add the button to the field that reads "none (object)";
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!
Your answer
Follow this Question
Related Questions
button action script 2 Answers
Sound stop when entering trigger 3 Answers
Loop Sounds when within trigger 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Can anyone help me figure this script out? (Light Trigger) 1 Answer