- Home /
Question by
TylerTimoJ · Mar 16, 2013 at 03:34 PM ·
buttonsoundplaypress
How To play sound with a keypress
Hello, I am trying to get a script for sound working. I know how to script with javascript pretty well but for some reason I cannot get a sound to play when I press a button. Here is my script, please tell me what I am doing wrong.
Note, I have an auido source attached to the object with this script.
@script RequireComponent(AudioSource)
var front : AudioClip;
var back : AudioClip;
function Update(){
if (Input.GetKeyDown("Fire1")) { audio.PlayOneShot(AudioSource.front, 1.0); }
if (Input.GetKeyDown("Fire2")) { audio.PlayOneShot(AudioSource.back, 1.0); } }
Comment
Answer by Cyberpie_n_milk · Mar 16, 2013 at 04:39 PM
This should work:
@script RequireComponent(AudioSource)
var front : AudioClip;
var back : AudioClip;
function Update(){
if (Input.GetButtonDown("Fire1")) { audio.PlayOneShot(front, 1.0); }
if (Input.GetButtonDown("Fire2")) { audio.PlayOneShot(back, 1.0); } }
Answer by nsxdavid · Mar 16, 2013 at 03:59 PM
Remove AudioSource
. from front
and back
Corrected:
if (Input.GetKeyDown("Fire1")) { audio.PlayOneShot(front, 1.0); }
if (Input.GetKeyDown("Fire2")) { audio.PlayOneShot(back, 1.0); } }