- Home /
Run and Walk Script not working
Hey everyone I have a walk/run script and I am getting an error which is stopping the script from working. I have looked through it and rewrote it and it still the same error the script I am using is:
#pragma strict
@script RequireComponent( AudioSource )
var walkSounds : AudioClip[];
var runSounds : AudioClip[];
var walkAudioSpeed : float = 0.4;
var runAudioSpeed : float = 0.2;
private var walkAudioTimer : float = 0.0;
private var runAudioTimer : float = 0.0;
var isWalking : boolean = false;
var isRunning : boolean = false;
var walkSpeed: float = 8; // regular speed
var runSpeed: float = 20; // run speed
private var chCtrl: CharacterController;
private var chMotor: CharacterMotor;
function Start()
{
chCtrl = GetComponent(CharacterController);
chMotor = GetComponent(CharacterMotor);
}
function Update()
{
SetSpeed();
if ( chCtrl.isGrounded )
{
PlayFootsteps();
}
else
{
walkAudioTimer = 1000.0;
runAudioTimer = 1000.0;
}
}
function SetSpeed()
{
var speed = walkSpeed;
if ( chCtrl.isGrounded && Input.GetKey("left shift") || Input.GetKey("right shift") )
{
speed = runSpeed;
}
chMotor.movement.maxForwardSpeed = speed;
}
function PlayFootsteps()
{
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;
}
// Play Audio
if ( isWalking )
{
if ( walkAudioTimer > walkAudioSpeed )
{
audio.Stop();
audio.clip = walkSounds[ Random.Range( 0, walkSounds.Length ) ];
audio.Play();
walkAudioTimer = 0.0;
}
}
else if ( isRunning )
{
if ( runAudioTimer > runAudioSpeed )
{
audio.Stop();
audio.clip = runSounds[ Random.Range( 0, runSounds.Length ) ];
audio.Play();
runAudioTimer = 0.0;
}
}
else
{
audio.Stop();
}
// increment timers
walkAudioTimer += Time.deltaTime;
runAudioTimer += Time.deltaTime;
}
If someone else could look over this maybe you can spot something I couldn't
Thanks
Edit Sorry Forgot the error code
NullReferenceException: Object reference not set to an instance of an object PlayerRunAndFootsteps.SetSpeed () (at Assets/Slender/Scripts/PlayerRunAndFootsteps.js:58) PlayerRunAndFootsteps.Update () (at Assets/Slender/Scripts/PlayerRunAndFootsteps.js:36)
Thank you here is the error code NullReferenceException: Object reference not set to an instance of an object PlayerRunAndFootsteps.SetSpeed () (at Assets/Slender/Scripts/PlayerRunAndFootsteps.js:58) PlayerRunAndFootsteps.Update () (at Assets/Slender/Scripts/PlayerRunAndFootsteps.js:36)
It seemed to stop working after I installed Unistorm before that it worked fine. I sorta need both of them
In your original .JS file the errors are in Update() line 36 and SetSpeed() line 58.
These line don't relate to your posted code. Can you advise the line numbers in the posted code that error.
Also any other information ( like the 'Unistorm' install ) that may be relevant may be handy.
I'm new to Unity and prefer cs to js so I may not actually be able to help you.
Object reference not set to an instance of an object PlayerRunAndFootsteps.SetSpeed () (at Assets/Slender/Scripts/PlayerRunAndFootsteps.js:52 PlayerRunAndFootsteps.Update () (at Assets/Slender/Scripts/PlayerRunAndFootsteps.js:30
Do you have a Character$$anonymous$$otor script attached to your component ? Have you made modifications to it ? Is there also one provided by the 'Unistorm' install ?
just a wildassedguess.
Answer by kdubnz · Nov 28, 2014 at 11:10 PM
Does this help ?
http://answers.unity3d.com/questions/373508/footsteps-script-for-running-and-walking.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613172537im_/https://answers.unity.com/themes/thub/images/avi.jpg)