Hello. I have a footsteps script I found, but I have a problem. Every time my frame updates, I have a single NullReferenceException. This eats up a HUGE amount of my game’s performance. Please help! Here the code:
#pragma strict
var inWater : boolean = false;
var waterStepSound : AudioClip;
var waterTimer : float = 0;
var waterCool : float = 0.6;
private var chMotor : CharacterMotor;
private var controller : CharacterController;
function Start()
{
chMotor = GetComponent(CharacterMotor);
controller = GetComponent(CharacterController);
}
function Update()
{
if(controller.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift))
{
chMotor.movement.maxForwardSpeed = 10;
chMotor.movement.maxSidewaysSpeed = 10;
}
else
{
chMotor.movement.maxForwardSpeed = 7;
chMotor.movement.maxSidewaysSpeed = 6;
}
if(controller.velocity.magnitude > 0 && inWater == true)
{
WaterSound();
}
if(controller.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift) && inWater == true)
{
waterCool = 0.4;
WaterSound();
}
else
{
waterCool = 0.6;
}
if(waterTimer > 0)
{
waterTimer -= Time.deltaTime;
}
if(waterTimer < 0)
{
waterTimer = 0;
}
}
function WaterSound()
{
if(waterTimer == 0)
{
audio.PlayOneShot(waterStepSound);
waterTimer = waterCool;
}
}
And the other script it is working with:
#pragma strict
private var footsteps : PlayerFootsteps;
function Start()
{
footsteps = GameObject.Find("First Person Controller").GetComponent(PlayerFootsteps);
}
function OnTriggerEnter (Col : Collider)
{
if(Col.gameObject.tag == "Player")
{
footsteps.inWater = true;
}
}
function OnTriggerExit (Col : Collider)
{
if(Col.gameObject.tag == "Player")
{
footsteps.inWater = false;
}
}