- Home /
sound on key down help
hi im new to scripting and this is my first attempt at writing my own script and it didn't work here it is
var soundFile:AudioClip;
function Update() {
if (Input.KeyDown("v"))
audio.clip = soundFile;
audio.Play();
}
if anyone can correct my script that would be great
thanks
Answer by ThomLaurent · Sep 29, 2014 at 06:03 PM
It seems you forgot to close your if statement after audio.Play();
, and the method Input.KeyDown()
doesn't exists so replace it with Input.GetKeyDown().
Here is the script corrected :
var soundFile : AudioClip;
function Update() {
if (Input.GetKeyDown("v")) {
audio.clip = soundFile;
audio.Play();
}
}
Now it should work :)
Answer by Eaneth · Sep 29, 2014 at 07:08 PM
The correct way to get keyboard input is: Input.GetKeyDown(KeyCode.#)
By the way, KeyCode is how you choose from the available keys. So use can use KeyCode.W, or KeyCode.Mouse0 to find the appropriate key for whatever you're using. Unity can't find the appropriate KeyCode value if you just supply a string, doesn't make any sense.
Wrong, Unity does support Input.Get$$anonymous$$eyDown(string)
, see documentation