Search Unity

NullReferenceException: Object reference not set to an instance of an object

Discussion in 'Animation' started by asteziin, Apr 30, 2022.

  1. asteziin

    asteziin

    Joined:
    Apr 30, 2022
    Posts:
    1
    Good afternoon friends, I'm suffering from some problems. my code was working perfectly until unity as ben crash gives some errors, can anyone help me?

    follow the errors and the code

    NullReferenceException: Object reference not set to an instance of an object
    PlayerBehaviour.Start () (at Assets/scripts/PlayerBehaviour.cs:52)

    NullReferenceException: Object reference not set to an instance of an object
    PlayerBehaviour.Update () (at Assets/scripts/PlayerBehaviour.cs:87)

    NullReferenceException: Object reference not set to an instance of an object
    PlayerBehaviour.Update () (at Assets/scripts/PlayerBehaviour.cs:75)




    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public enum TypeCharacter
    5. {
    6.     Warrior = 0,
    7.     Wizard = 1,
    8.     Archer = 2
    9. }
    10.  
    11. public enum PlayerStates
    12. {
    13.     Movement,
    14.     Attack,
    15. }
    16.  
    17. public class PlayerBehaviour : CharacterBase
    18. {
    19.  
    20.     private TypeCharacter type;
    21.  
    22.     private AnimationController animationController;
    23.  
    24.     //StateMachine
    25.     private PlayerStates currentState = PlayerStates.Movement;
    26.     //
    27.  
    28.     //Movimentation
    29.     private float speed = 3.0F;
    30.     private float speedSideWalk = 3.0F;
    31.     public float speedRun;
    32.     public float speedWalk;
    33.     public float rotateSpeed = 3.0F;
    34.     public CharacterController controller;
    35.     public Transform focusCamera;
    36.     //
    37.  
    38.     //Attack
    39.     private int currentAttack = 0;
    40.     public float attackRate;
    41.     public int totalAttackAnimations;
    42.     private float currentAttackRate;
    43.     //
    44.  
    45.     new protected void Start()
    46.     {
    47.        
    48.         PlayerStatsController.SetTypeCharacter(TypeCharacter.Wizard);
    49.         currentLevel = PlayerStatsController.GetCurrentLevel();
    50.         type = PlayerStatsController.GetTypeCharacter();
    51.  
    52.         basicStats = PlayerStatsController.intance.GetBasicStats(type);
    53.  
    54.         animationController = GetComponent<AnimationController>();
    55.         speed = speedWalk;
    56.  
    57.         controller = GetComponent<CharacterController>();
    58.  
    59.         base.Start();
    60.     }
    61.  
    62.         // Update is called once per frame
    63.         new void Update()
    64.     {
    65.         base.Update();
    66.  
    67.         switch (currentState)
    68.         {
    69.             case PlayerStates.Movement:
    70.                 {
    71.  
    72.                     if (Input.GetKey(KeyCode.LeftShift) || Input.GetAxis("Vertical") != 0)
    73.                     {
    74.                         speed = speedRun;
    75.                         animationController.PlayAnimation(AnimationStates.RUN);
    76.                     }
    77.                     else
    78.                     {
    79.                         speed = speedWalk;
    80.                         if (Input.GetAxis("Vertical") != 0)
    81.                             animationController.PlayAnimation(AnimationStates.WALK);
    82.                         else if (Input.GetAxis("Horizontal") != 0)
    83.                         {
    84.                             animationController.PlayAnimation(AnimationStates.SIDE_WALK);
    85.                         }
    86.                         else
    87.                             animationController.PlayAnimation(AnimationStates.IDDLE);
    88.                     }
    89.  
    90.  
    91.                     if (Mathf.Abs(Input.GetAxis("Vertical")) > 0)
    92.                     {
    93.  
    94.                         transform.LookAt(new Vector3(focusCamera.position.x, transform.position.y, focusCamera.position.z));
    95.                         Vector3 direction = transform.TransformDirection(Vector3.forward);
    96.                         float curSpeed = speed * Input.GetAxis("Vertical");
    97.                         controller.SimpleMove(direction * curSpeed);
    98.                     }
    99.  
    100.                     if (Mathf.Abs(Input.GetAxis("Horizontal")) > 0)
    101.                     {
    102.                         transform.LookAt(new Vector3(focusCamera.position.x, transform.position.y, focusCamera.position.z));
    103.                         Vector3 direction = transform.TransformDirection(Vector3.right);
    104.                         float curSpeed = speedSideWalk * Input.GetAxis("Horizontal");
    105.                         controller.SimpleMove(direction * curSpeed);
    106.  
    107.                     }
    108.  
    109.                     //Attack Change State
    110.                     if (Input.GetKey(KeyCode.LeftControl))
    111.                     {
    112.                         currentState = PlayerStates.Attack;
    113.                     }
    114.  
    115.                 }
    116.                 break;
    117.             case PlayerStates.Attack:
    118.                 {
    119.                     currentAttackRate += Time.deltaTime;
    120.  
    121.                     if (currentAttackRate > attackRate)
    122.                     {
    123.                         currentAttackRate = 0;
    124.                         animationController.CallAttackAnimation(currentAttack);
    125.                         currentAttack++;
    126.  
    127.                         if (currentAttack > totalAttackAnimations)
    128.                         {
    129.                             currentAttack = 0;
    130.                         }
    131.                     }
    132.  
    133.                     if (!Input.GetKey(KeyCode.LeftControl))
    134.                     {
    135.                         currentState = PlayerStates.Movement;
    136.                         currentAttackRate = 0;
    137.                     }
    138.  
    139.                 }
    140.                 break;
    141.  
    142.         }
    143.  
    144.     }
    145. }
    146.  
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599