NullReferenceException on a javascript file

Hi all! :slight_smile:
I got today a problem with a javascript file on my Player. This is a stamina-sprint script.

#pragma strict
@script RequireComponent( AudioSource )
public var walkSounds : AudioClip[];
public var runSounds : AudioClip[];
public var walkAudioSpeed : float = 0.4;
public var runAudioSpeed : float = 0.2;
private var walkAudioTimer : float = 0.0;
private var runAudioTimer : float = 0.0;
var isWalking : boolean = false; // these can be private
var isRunning : boolean = false; // these can be private
public var walkSpeed : float = 8; // regular speed
public var runSpeed : float = 20; // run speed
public var sprintDuration : float = 10.0;
private var sprintTimer : float = 0;
private var stamina : float = 0.0;
public var staminaTexture : Texture;
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 ( 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;
    }
    // check the sprint timer
    if ( isRunning )
    {
        sprintTimer += Time.deltaTime;
        if ( sprintTimer > sprintDuration )
        {
            isRunning = false;
            isWalking = true;
        }
        else if ( chCtrl.isGrounded )
        {
            speed = runSpeed;
        }
    }
    else
    {
        sprintTimer -= Time.deltaTime;
    }
    sprintTimer = Mathf.Clamp( sprintTimer, 0, sprintDuration );
    chMotor.movement.maxForwardSpeed = speed;
    stamina = 1.0 - ( sprintTimer / sprintDuration );
}
function PlayFootsteps()
{
    // 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;
}
function OnGUI()
{
    if ( staminaTexture ) // check there is a texture so it doesn't error if not
    {
        GUI.DrawTexture( Rect( 15, Screen.height - 20, 100 * stamina, 12 ), staminaTexture );
    }
}

And I attached a screen capture about the issue.
(Sorry for the big object mess :roll_eyes: )

The sprint doesn’t work, and the stamina texture won’t show up, the sound effect won’t play.
The script detects when I press the left shift, (see on the picture below at the is walking, is running). But my charater won’t sprint.

The error tells you something is null
Look at the line number (it says 103, but you have cropped the posted code so i don’t know)
Debug.Log the variables in line 103 to find out which one is null

My bad, there is the right code, and I attached the new picture about the issue.
From what can this happened? I don’t do anything with this script, or with my character.
I just placed some objects (models etc.) on the terrain.
And when I open up a new project and played this script on the character, It worked fine.

#pragma strict
@script RequireComponent( AudioSource )
public var walkSounds : AudioClip[];
public var runSounds : AudioClip[];
public var walkAudioSpeed : float = 0.4;
public var runAudioSpeed : float = 0.2;
private var walkAudioTimer : float = 0.0;
private var runAudioTimer : float = 0.0;
var isWalking : boolean = false; // these can be private
var isRunning : boolean = false; // these can be private
public var walkSpeed : float = 8; // regular speed
public var runSpeed : float = 20; // run speed
public var sprintDuration : float = 10.0;
private var sprintTimer : float = 0;
private var stamina : float = 0.0;
public var staminaTexture : Texture;
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 ( 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;
    }
    // check the sprint timer
    if ( isRunning )
    {
        sprintTimer += Time.deltaTime;
        if ( sprintTimer > sprintDuration )
        {
            isRunning = false;
            isWalking = true;
        }
        else if ( chCtrl.isGrounded )
        {
            speed = runSpeed;
        }
    }
    else
    {
        sprintTimer -= Time.deltaTime;
    }
    sprintTimer = Mathf.Clamp( sprintTimer, 0, sprintDuration );
    chMotor.movement.maxForwardSpeed = speed;
    stamina = 1.0 - ( sprintTimer / sprintDuration );
}
function PlayFootsteps()
{
    // 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;
}
function OnGUI()
{
    if ( staminaTexture ) // check there is a texture so it doesn't error if not
    {
        GUI.DrawTexture( Rect( 15, Screen.height - 20, 100 * stamina, 12 ), staminaTexture );
    }
}