FPS Audio Issue
Hi, Im trying to make footstep audio when i walk in my game. But here is my error:
MissingComponentException: There is no 'AudioSource' attached to the "First Person Controller" game object, but a script is trying to access it. You probably need to add a AudioSource to the game object "First Person Controller". Or your script needs to check if the component is attached before using it. FootStepsOn.PlayAudio () (at Assets/FootStepsOn.js:71) FootStepsOn.Update () (at Assets/FootStepsOn.js:11)
Here is my code:
var walk : AudioClip;
var run : AudioClip;
var isWalking : boolean = false;
var isRunning : boolean = false;
function Update()
{
GetState();
PlayAudio();
}
function GetState()
{
if ( Input.GetAxis( "Horizontal" ) || Input.GetAxis( "Vertical" ) )
{
if ( Input.GetKey( "left shift" ) || Input.GetKey( "right shift" ) )
{
// Running
isWalking = false;
isRunning = true;
}
else
{
// Walking
isWalking = true;
isRunning = false;
}
}
else
{
// Stopped
isWalking = false;
isRunning = false;
}
}
function PlayAudio()
{
if ( isWalking )
{
if ( GetComponent.<AudioSource>().clip != walk )
{
GetComponent.<AudioSource>().Stop();
GetComponent.<AudioSource>().clip = walk;
}
if ( !GetComponent.<AudioSource>().isPlaying )
{
GetComponent.<AudioSource>().Play();
}
}
else if ( isRunning )
{
if ( GetComponent.<AudioSource>().clip != run )
{
GetComponent.<AudioSource>().Stop();
GetComponent.<AudioSource>().clip = run;
}
if ( !GetComponent.<AudioSource>().isPlaying )
{
GetComponent.<AudioSource>().Play();
}
}
else
{
GetComponent.<AudioSource>().Stop();
}
}
AND here is an image that i attached the audio together:
Answer by mrmightypants · Oct 17, 2015 at 02:10 PM
Your screen shot doesn't show whether or not you have an AudioSource component, can you confirm whether or not that is present?
Your answer
Follow this Question
Related Questions
Trying to add walking sound effects into my game 1 Answer
Unity noob here, need help adding footstep logic 1 Answer
Can't figure out how to play a random audio clip when player collides with enemy 1 Answer
How do I play an audio clip on a keyframe in sprite animation? 0 Answers
Need help perfecting Siren Audio Effect 0 Answers