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: