- Home /
Why does unity3D crash when starting this script in play mode?
When I click play in unity3D the engine starts my game until I press "W" key to walk forward. My goal is to use the script below to have a foot steps sound loop while "W" key is pressed.
NOTE I am not very skilled at scripting just started using unity 1 month ago.
Here's my script:
#pragma strict
var footstepSound : AudioClip;
function Update () {
if (Input.GetKey(KeyCode.W) && !audio.isPlaying){
audio.clip = footstepSound;
audio.Play();
}
}
The script is fine. You do have an AudioSource component on this same game object? Does it work if you change the audio clip to something else?
I have an attached audio file and no it does not work with a different audio file.
I've converted robertbu's comment into an answer, since you said it solved your problem. Please mark it as the accepted answer to close this question properly. Thanks!
Answer by robertbu · Jul 24, 2014 at 06:08 PM
First off, put your script in a new scene, attach it to an empty game object, add an AudioSource, drag and drop a clip onto the 'footstepSound' variable, and hit play. Second, the screen shot did not tell me anything. So small, it is difficult to read, but 1) I don't see an AudioSource in the components attached to the object you selected in the Hierarchy, and 2) you don't have a console window showing, so we cannot see what (if any) errors are in your code.
Oh, adding an audiosource to the player worked thank you.