I’m pretty new to Unity, and I don’t have much experience with scripting, so I was wondering if anyone could help with a problem I have…
I’m trying to get an audio clip to play whenever the key “f” is pressed on the keyboard, as well as an animation. Here’s the code:
function Update () {
if (Input.GetKeyDown ("f")) {
animation.Play ("Flight");
audio.Play("Flight_AudioClip")
}
}
So far, the animation works absolutely fine, but the audio refuses to play, and the error box comes up with “Assets/Scripts/Flight.js(5,27): BCE0017: The best overload for the method ‘UnityEngine.AudioSource.Play()’ is not compatible with the argument list ‘(String)’.”
I have no idea what this means, I’ve tried replacing “audio.Play” with heaps of other things, looked on the net for templates of scripts to use, but I still can’t find any sort of answer…
First of all you will need to add a audio source to the gameObject that is supposed to play the audio clip.
I would do this instead:
var canPlayAudio = true;
var waitTime = 1.0; // Change this to the approximate time the audio clip takes to finish.
var MySound : AudioClip; //Drag your audio clip here in the Inspector.
function Update ()
{
AudioController();
}
function AudioController();
{
if (Input.GetKeyDown ("f") && canPlayAudio)
{
canPlayAudio = false;
animation.Play ("Flight");
audio.PlayOneShot(MySound);
yield WaitForSeconds(waitTime);
canPlayAudio = true;
}
}
The thing is you’re using it wrong. You need an instance of an AudioSource first, in the scripting reference this is often called audio. So audio is meant to be a variable. Please review the following pages to get an understanding of how the setup should be: