- Home /
NullReferenceException Confusion
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;
     }
 }
                                                           
Ok. Here it is: NullReferenceException $$anonymous$$ovementscript.FixedUpdate () (at Assets/Footsteps.js:28) in the first script
Answer by KellyThomas · Apr 27, 2014 at 05:37 PM
It looks like chMotor is null when executing that line.
This would be the case is GetComponent(CharacterMotor) returns null in Start().
Is a CharacterMotor script attached to this GameObject? Is the script disabled? 
Yes, a Character$$anonymous$$otor is attached to the GameObject. Is that messing it up?
Your answer
 
 
             Follow this Question
Related Questions
Problems making an audio JumpScare 2 Answers
Audio not working With Compare Tag 0 Answers
pause a audio inside game 1 Answer
Audio Not working 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                