- Home /
Using input.getbuttondown to play audio = NOT WORKING? :(
I'm having the hardest time figuring out what's wrong here. When I press mouse 0 (which I set as "Attack1" in Input) I want it to play my attack sound. I've given my character an audio source, and I've given him this script:
var sound : AudioClip;
function Update (){
if(Input.GetButtonDown("Attack1")){
audio.Play();
}
}
I've also dragged the corresponding audio file to the target slot in the inspector.
I don't know why this isn't playing :( I've checked the sound to make sure it even played anything, and it does. All the names are correct and all the capitals are in the right spot.
I know very little about scripts but I've done a lot of research and from the looks of things, this ought to do it?
What am I missing?
Answer by Soapies · Apr 17, 2012 at 08:32 AM
fafase! You are a genius. That worked... but why would that make any difference? :/
Thank you so much!
Weel simply put, an audio source means a speaker (opposed to audio listener, a microphone). But a speaker is nothing without a sound, that is the AudioClip. clip is the defaul variable of the AudioClip. In you case, you could have tried AudioSource.Play() and I guess it would have worked but it is no proper way (to me).
With audio.clip = sound; you tell the compiler that for the audio you want that sound. You can now even change it later on, if for instance, you want your guy to emit a different sound depending on its health.
var soundHealthy : AudioClip;
var soundDying :AudioClip;
function Start(){
audio.clip = soundHealthy;}
function Update(){
audio.Play();
if(dying)
audio.clip = soundDying; }
By the way please post replies as comment not as an answer.Also, you should mark it as answered.