- Home /
Footstep Script (NullReferenceException: Object reference not set to an instance of an object)
Hello! I have finally found a footstep script that works very well for me. It plays the footstep sounds correctly, by how long I want the audio length to be and through the character controller's velocity.
But once I'm done walking and stay still, it restarts the audio file and plays the ENTIRE audio clip (which is a grass footstep audio clip that is 55 seconds long) when I'm not moving.
Is there something I have to change in the script? Or should I just altogether find separate, small few second audio clips for parts of the footstep sound?
Here is the script:
#pragma strict
/**
* Script made by OMA [www.oma.netau.net]
**/
var concrete : AudioClip[];
var wood : AudioClip[];
var dirt : AudioClip[];
var metal : AudioClip[];
var glass : AudioClip[];
var sand: AudioClip [];
var snow: AudioClip [];
var floor: AudioClip [];
var grass: AudioClip [];
private var step : boolean = true;
var audioStepLengthWalk : float = 0.7;
var audioStepLengthRun : float = 0.25;
function OnControllerColliderHit (hit : ControllerColliderHit) {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Concrete" && step == true || controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Untagged" && step == true ) {
WalkOnConcrete();
} else if (controller.isGrounded && controller.velocity.magnitude > 8 && hit.gameObject.tag == "Concrete" && step == true || controller.isGrounded && controller.velocity.magnitude > 8 && hit.gameObject.tag == "Untagged" && step == true) {
RunOnConcrete();
} else if (controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Wood" && step == true) {
WalkOnWood();
} else if (controller.isGrounded && controller.velocity.magnitude > 8 && hit.gameObject.tag == "Wood" && step == true) {
RunOnWood();
} else if (controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Dirt" && step == true) {
WalkOnDirt();
} else if (controller.isGrounded && controller.velocity.magnitude > 8 && hit.gameObject.tag == "Dirt" && step == true) {
RunOnDirt();
} else if (controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Metal" && step == true) {
WalkOnMetal();
} else if (controller.isGrounded && controller.velocity.magnitude > 8 && hit.gameObject.tag == "Metal" && step == true) {
RunOnMetal();
} else if (controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Glass" && step == true) {
WalkOnGlass();
} else if (controller.isGrounded && controller.velocity.magnitude > 8 && hit.gameObject.tag == "Glass" && step == true) {
RunOnGlass();
} else if (controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Sand" && step == true) {
WalkOnSand();
} else if (controller.isGrounded && controller.velocity.magnitude > 8 && hit.gameObject.tag == "Sand" && step == true) {
RunOnSand();
} else if (controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Snow" && step == true) {
WalkOnSnow();
} else if (controller.isGrounded && controller.velocity.magnitude > 8 && hit.gameObject.tag == "Snow" && step == true) {
RunOnSnow();
} else if (controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Floor" && step == true) {
WalkOnFloor();
} else if (controller.isGrounded && controller.velocity.magnitude > 8 && hit.gameObject.tag == "Floor" && step == true) {
RunOnFloor();
} else if (controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Grass" && step == true) {
WalkOnGrass();
} else if (controller.isGrounded && controller.velocity.magnitude > 8 && hit.gameObject.tag == "Grass" && step == true) {
RunOnGrass();
}
}
/////////////////////////////////// CONCRETE ////////////////////////////////////////
function WalkOnConcrete() {
step = false;
audio.clip = concrete[Random.Range(0, concrete.length)];
audio.volume = .1;
audio.Play();
yield WaitForSeconds (audioStepLengthWalk);
step = true;
}
function RunOnConcrete() {
step = false;
audio.clip = concrete[Random.Range(0, concrete.length)];
audio.volume = .3;
audio.Play();
yield WaitForSeconds (audioStepLengthRun);
step = true;
}
////////////////////////////////// WOOD /////////////////////////////////////////////
function WalkOnWood() {
step = false;
audio.clip = wood[Random.Range(0, wood.length)];
audio.volume = .1;
audio.Play();
yield WaitForSeconds (audioStepLengthWalk);
step = true;
}
function RunOnWood() {
step = false;
audio.clip = wood[Random.Range(0, wood.length)];
audio.volume = .3;
audio.Play();
yield WaitForSeconds (audioStepLengthRun);
step = true;
}
/////////////////////////////////// DIRT //////////////////////////////////////////////
function WalkOnDirt() {
step = false;
audio.clip = dirt[Random.Range(0, dirt.length)];
audio.volume = .1;
audio.Play();
yield WaitForSeconds (audioStepLengthWalk);
step = true;
}
function RunOnDirt() {
step = false;
audio.clip = dirt[Random.Range(0, dirt.length)];
audio.volume = .3;
audio.Play();
yield WaitForSeconds (audioStepLengthRun);
step = true;
}
////////////////////////////////// METAL ///////////////////////////////////////////////
function WalkOnMetal() {
step = false;
audio.clip = metal[Random.Range(0, metal.length)];
audio.volume = .1;
audio.Play();
yield WaitForSeconds (audioStepLengthWalk);
step = true;
}
function RunOnMetal() {
step = false;
audio.clip = metal[Random.Range(0, metal.length)];
audio.volume = .3;
audio.Play();
yield WaitForSeconds (audioStepLengthRun);
step = true;
}
////////////////////////////////// GLASS ///////////////////////////////////////////////
function WalkOnGlass() {
step = false;
audio.clip = glass[Random.Range(0, glass.length)];
audio.volume = .1;
audio.Play();
yield WaitForSeconds (audioStepLengthWalk);
step = true;
}
function RunOnGlass() {
step = false;
audio.clip = glass[Random.Range(0, glass.length)];
audio.volume = .3;
audio.Play();
yield WaitForSeconds (audioStepLengthRun);
step = true;
}
////////////////////////////////// SAND ///////////////////////////////////////////////
function WalkOnSand() {
step = false;
audio.clip = sand[Random.Range(0, sand.length)];
audio.volume = .1;
audio.Play();
yield WaitForSeconds (audioStepLengthWalk);
step = true;
}
function RunOnSand() {
step = false;
audio.clip = sand[Random.Range(0, sand.length)];
audio.volume = .3;
audio.Play();
yield WaitForSeconds (audioStepLengthRun);
step = true;
}
////////////////////////////////// SNOW ///////////////////////////////////////////////
function WalkOnSnow() {
step = false;
audio.clip = snow[Random.Range(0, snow.length)];
audio.volume = .1;
audio.Play();
yield WaitForSeconds (audioStepLengthWalk);
step = true;
}
function RunOnSnow() {
step = false;
audio.clip = snow[Random.Range(0, snow.length)];
audio.volume = .3;
audio.Play();
yield WaitForSeconds (audioStepLengthRun);
step = true;
}
////////////////////////////////// FLOOR ///////////////////////////////////////////////
function WalkOnFloor() {
step = false;
audio.clip = floor[Random.Range(0, floor.length)];
audio.volume = .1;
audio.Play();
yield WaitForSeconds (audioStepLengthWalk);
step = true;
}
function RunOnFloor() {
step = false;
audio.clip = floor[Random.Range(0, floor.length)];
audio.volume = .3;
audio.Play();
yield WaitForSeconds (audioStepLengthRun);
step = true;
}
////////////////////////////////// GRASS ///////////////////////////////////////////////
function WalkOnGrass() {
step = false;
audio.clip = grass[Random.Range(0, grass.length)];
audio.volume = .1;
audio.Play();
yield WaitForSeconds (audioStepLengthWalk);
step = true;
}
function RunOnGrass() {
step = false;
audio.clip = grass[Random.Range(0, grass.length)];
audio.volume = .3;
audio.Play();
yield WaitForSeconds (audioStepLengthRun);
step = true;
}
@script RequireComponent(AudioSource)
There's no reason to be using a 55 second long clip in the first place. You don't need to find new clips, either. You can find a free audio editing program for any platform that will let you crop out any portion of any audio file.
Answer by getyour411 · Feb 01, 2014 at 08:33 PM
I would not advise this
function OnControllerColliderHit (hit : ControllerColliderHit) {
var controller : CharacterController = GetComponent(CharacterController);
I would move the assignment of controller out of this block since it's incurring overhead on every call to do that Find/GetComponent bit.
I glanced through this and didn't see relevant code that's executed when the controller is at rest (no velocity). If it's in there somewhere, use audioStop
If it's not in there, add it.
I've made little 1 second parts of the long footstep audio clip, which equals to a size of 4 select audio clips I have chosen. But when I walk, it will only play 1 sound, then stop playing anything altogether in my continuation of walking. Once I stop, when I make one step again, only 1 sound will play at the beginning again.
Is there a way I can have the script to say "Play all of these audio clips until the character stops moving"?
I'm using this new script and it works well, but when I add in the character controller velocity into the script I get this:
NullReferenceException: Object reference not set to an instance of an object
PlayerRunTimer&Footsteps.PlayFootsteps () (at Assets/_Scripts/PlayerRunTimer&Footsteps.js:77)
PlayerRunTimer&Footsteps.Update () (at Assets/_Scripts/PlayerRunTimer&Footsteps.js:33)
Here is the newer script from Alucardj:
// PlayerRunWTimerAndFootsteps.js //
// Written by Alucard $$anonymous$$ //
// 5/10/2013 //
//------------------------------//
#pragma strict
@script RequireComponent( AudioSource )
var walkSounds : AudioClip[];
var walkAudioSpeed : float = 0.7;
private var walkAudioTimer : float = 0.0;
var isWalking : boolean = false;
var isRunning : boolean = false;
private var chCtrl: CharacterController;
function Start()
{
chCtrl = GetComponent(CharacterController);
}
function Update()
{
Deter$$anonymous$$eState();
if ( chCtrl.isGrounded )
{
PlayFootsteps();
}
else
{
walkAudioTimer = walkAudioSpeed;
}
}
function Deter$$anonymous$$eState()
{
// increment timers
walkAudioTimer += Time.deltaTime;
if ( Input.GetAxis( "Horizontal" ) || Input.GetAxis( "Vertical" ) )
{
if ( Input.Get$$anonymous$$ey( "left shift" ) || Input.Get$$anonymous$$ey( "right shift" ) )
{
// Running
isWalking = false;
isRunning = true;
}
else
{
// Walking
isWalking = true;
isRunning = false;
}
}
else
{
// Stopped
isWalking = false;
isRunning = false;
}
}
function PlayFootsteps()
{
var hit: ControllerColliderHit;
// Play Audio
if ( isWalking )
{
if ( walkAudioTimer > walkAudioSpeed )
{
if ( chCtrl.isGrounded && chCtrl.velocity.magnitude < 10 && chCtrl.velocity.magnitude > 5 && hit.gameObject.tag == "Untagged" )
{
audio.Stop();
audio.clip = walkSounds[ Random.Range( 0, walkSounds.Length ) ];
audio.Play();
walkAudioTimer = 0.0;
}
}
}
else
{
audio.Stop();
}
}
Does anybody know what this means and how I can fix it?
Your answer
Follow this Question
Related Questions
AudioSource not repeating at correct times 0 Answers
Audio for movement 1 Answer
Random footsteps 5 Answers