Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

NullReferenceException on a javascript file

Discussion in 'Scripting' started by Pixfloage, Aug 29, 2014.

  1. Pixfloage

    Pixfloage

    Joined:
    Aug 18, 2014
    Posts:
    12
    Hi all! :)
    I got today a problem with a javascript file on my Player. This is a stamina-sprint script.
    Code (JavaScript):
    1. #pragma strict
    2. @script RequireComponent( AudioSource )
    3. public var walkSounds : AudioClip[];
    4. public var runSounds : AudioClip[];
    5. public var walkAudioSpeed : float = 0.4;
    6. public var runAudioSpeed : float = 0.2;
    7. private var walkAudioTimer : float = 0.0;
    8. private var runAudioTimer : float = 0.0;
    9. var isWalking : boolean = false; // these can be private
    10. var isRunning : boolean = false; // these can be private
    11. public var walkSpeed : float = 8; // regular speed
    12. public var runSpeed : float = 20; // run speed
    13. public var sprintDuration : float = 10.0;
    14. private var sprintTimer : float = 0;
    15. private var stamina : float = 0.0;
    16. public var staminaTexture : Texture;
    17. private var chCtrl: CharacterController;
    18. private var chMotor: CharacterMotor;
    19. function Start()
    20. {
    21.     chCtrl = GetComponent(CharacterController);
    22.     chMotor = GetComponent(CharacterMotor);
    23. }
    24. function Update()
    25. {
    26.     SetSpeed();
    27.     if ( chCtrl.isGrounded )
    28.     {
    29.         PlayFootsteps();
    30.     }
    31.     else
    32.     {
    33.         walkAudioTimer = 1000.0;
    34.         runAudioTimer = 1000.0;
    35.     }
    36. }
    37. function SetSpeed()
    38. {
    39.     var speed = walkSpeed;
    40.     if ( Input.GetAxis( "Horizontal" ) || Input.GetAxis( "Vertical" ) )
    41.     {
    42.         if ( Input.GetKey( "left shift" ) || Input.GetKey( "right shift" ) )
    43.         {
    44.             // Running
    45.             isWalking = false;
    46.             isRunning = true;
    47.         }
    48.         else
    49.         {
    50.             // Walking
    51.             isWalking = true;
    52.             isRunning = false;
    53.         }
    54.     }
    55.     else
    56.     {
    57.         // Stopped
    58.         isWalking = false;
    59.         isRunning = false;
    60.     }
    61.     // check the sprint timer
    62.     if ( isRunning )
    63.     {
    64.         sprintTimer += Time.deltaTime;
    65.         if ( sprintTimer > sprintDuration )
    66.         {
    67.             isRunning = false;
    68.             isWalking = true;
    69.         }
    70.         else if ( chCtrl.isGrounded )
    71.         {
    72.             speed = runSpeed;
    73.         }
    74.     }
    75.     else
    76.     {
    77.         sprintTimer -= Time.deltaTime;
    78.     }
    79.     sprintTimer = Mathf.Clamp( sprintTimer, 0, sprintDuration );
    80.     chMotor.movement.maxForwardSpeed = speed;
    81.     stamina = 1.0 - ( sprintTimer / sprintDuration );
    82. }
    83. function PlayFootsteps()
    84. {
    85.     // Play Audio
    86.     if ( isWalking )
    87.     {
    88.         if ( walkAudioTimer > walkAudioSpeed )
    89.         {
    90.             audio.Stop();
    91.             audio.clip = walkSounds[ Random.Range( 0, walkSounds.Length ) ];
    92.             audio.Play();
    93.             walkAudioTimer = 0.0;
    94.         }
    95.     }
    96.     else if ( isRunning )
    97.     {
    98.         if ( runAudioTimer > runAudioSpeed )
    99.         {
    100.             audio.Stop();
    101.             audio.clip = runSounds[ Random.Range( 0, runSounds.Length ) ];
    102.             audio.Play();
    103.             runAudioTimer = 0.0;
    104.         }
    105.     }
    106.     else
    107.     {
    108.         audio.Stop();
    109.     }
    110.     // increment timers
    111.     walkAudioTimer += Time.deltaTime;
    112.     runAudioTimer += Time.deltaTime;
    113. }
    114. function OnGUI()
    115. {
    116.     if ( staminaTexture ) // check there is a texture so it doesn't error if not
    117.     {
    118.         GUI.DrawTexture( Rect( 15, Screen.height - 20, 100 * stamina, 12 ), staminaTexture );
    119.     }
    120. }
    And I attached a screen capture about the issue.
    (Sorry for the big object mess :rolleyes: )

    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.
     

    Attached Files:

  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    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
     
  3. Pixfloage

    Pixfloage

    Joined:
    Aug 18, 2014
    Posts:
    12
    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.
    Code (JavaScript):
    1. #pragma strict
    2. @script RequireComponent( AudioSource )
    3. public var walkSounds : AudioClip[];
    4. public var runSounds : AudioClip[];
    5. public var walkAudioSpeed : float = 0.4;
    6. public var runAudioSpeed : float = 0.2;
    7. private var walkAudioTimer : float = 0.0;
    8. private var runAudioTimer : float = 0.0;
    9. var isWalking : boolean = false; // these can be private
    10. var isRunning : boolean = false; // these can be private
    11. public var walkSpeed : float = 8; // regular speed
    12. public var runSpeed : float = 20; // run speed
    13. public var sprintDuration : float = 10.0;
    14. private var sprintTimer : float = 0;
    15. private var stamina : float = 0.0;
    16. public var staminaTexture : Texture;
    17. private var chCtrl: CharacterController;
    18. private var chMotor: CharacterMotor;
    19. function Start()
    20. {
    21.     chCtrl = GetComponent(CharacterController);
    22.     chMotor = GetComponent(CharacterMotor);
    23. }
    24. function Update()
    25. {
    26.     SetSpeed();
    27.     if ( chCtrl.isGrounded )
    28.     {
    29.         PlayFootsteps();
    30.     }
    31.     else
    32.     {
    33.         walkAudioTimer = 1000.0;
    34.         runAudioTimer = 1000.0;
    35.     }
    36. }
    37. function SetSpeed()
    38. {
    39.     var speed = walkSpeed;
    40.     if ( Input.GetAxis( "Horizontal" ) || Input.GetAxis( "Vertical" ) )
    41.     {
    42.         if ( Input.GetKey( "left shift" ) || Input.GetKey( "right shift" ) )
    43.         {
    44.             // Running
    45.             isWalking = false;
    46.             isRunning = true;
    47.         }
    48.         else
    49.         {
    50.             // Walking
    51.             isWalking = true;
    52.             isRunning = false;
    53.         }
    54.     }
    55.     else
    56.     {
    57.         // Stopped
    58.         isWalking = false;
    59.         isRunning = false;
    60.     }
    61.     // check the sprint timer
    62.     if ( isRunning )
    63.     {
    64.         sprintTimer += Time.deltaTime;
    65.         if ( sprintTimer > sprintDuration )
    66.         {
    67.             isRunning = false;
    68.             isWalking = true;
    69.         }
    70.         else if ( chCtrl.isGrounded )
    71.         {
    72.             speed = runSpeed;
    73.         }
    74.     }
    75.     else
    76.     {
    77.         sprintTimer -= Time.deltaTime;
    78.     }
    79.     sprintTimer = Mathf.Clamp( sprintTimer, 0, sprintDuration );
    80.     chMotor.movement.maxForwardSpeed = speed;
    81.     stamina = 1.0 - ( sprintTimer / sprintDuration );
    82. }
    83. function PlayFootsteps()
    84. {
    85.     // Play Audio
    86.     if ( isWalking )
    87.     {
    88.         if ( walkAudioTimer > walkAudioSpeed )
    89.         {
    90.             audio.Stop();
    91.             audio.clip = walkSounds[ Random.Range( 0, walkSounds.Length ) ];
    92.             audio.Play();
    93.             walkAudioTimer = 0.0;
    94.         }
    95.     }
    96.     else if ( isRunning )
    97.     {
    98.         if ( runAudioTimer > runAudioSpeed )
    99.         {
    100.             audio.Stop();
    101.             audio.clip = runSounds[ Random.Range( 0, runSounds.Length ) ];
    102.             audio.Play();
    103.             runAudioTimer = 0.0;
    104.         }
    105.     }
    106.     else
    107.     {
    108.         audio.Stop();
    109.     }
    110.     // increment timers
    111.     walkAudioTimer += Time.deltaTime;
    112.     runAudioTimer += Time.deltaTime;
    113. }
    114. function OnGUI()
    115. {
    116.     if ( staminaTexture ) // check there is a texture so it doesn't error if not
    117.     {
    118.         GUI.DrawTexture( Rect( 15, Screen.height - 20, 100 * stamina, 12 ), staminaTexture );
    119.     }
    120. }
     

    Attached Files:

    • fix.PNG
      fix.PNG
      File size:
      26.3 KB
      Views:
      660