How to add sound to when a key is pressed

I already searched it on google, and couldn’t find anything that could help me out. I want to make it so when I press the ‘F’ key it gives a sound. How can I make that.

-Chris

Let’s assume that you’ve already imported an AudioClip and attached an AudioSource to a GameObject. If not, check through the manual.

Let’s make a quick class in C#:

[RequireComponent(typeof(AudioSource))]
public class PlaySoundF : MonoBehaviour {
    void Update() {
        if (Input.GetKeyDown(KeyCode.F)) {
            audio.Play();
        }
    }
}

After we save the script, we can drag it onto an object in the scene, set up the corresponding AudioSource, and see if it works.

Scripting reference pages:

If you find all of this terribly confusing, now is a good time to look up a good “getting started scripting in Unity” tutorial.