Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Need help with NullReferenceException: Object reference not set to an instance of an object

Discussion in 'Scripting' started by Wilc0, Aug 10, 2022.

  1. Wilc0

    Wilc0

    Joined:
    Jun 3, 2020
    Posts:
    20
    NullReferenceException: Object reference not set to an instance of an object
    InputManager.HandleMovementInput () (at Assets/Scripts/InputManager.cs:46)
    InputManager.HandleAllInputs () (at Assets/Scripts/InputManager.cs:38)
    PlayerManager.Update () (at Assets/Scripts/PlayerManager.cs:16)


    Im having an issue where this is returning null. Ive checked the code and have no errors in vs code. I was hoping someone could help me out.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerManager : MonoBehaviour
    4. {
    5.     InputManager inputManager;
    6.     PlayerLocomotion playerLocomotion;
    7.    
    8.     private void Awake()
    9.         {
    10.             inputManager = GetComponent<InputManager>();
    11.             playerLocomotion = GetComponent<PlayerLocomotion>();
    12.         }
    13.  
    14.         private void Update()
    15.         {
    16.             inputManager.HandleAllInputs();
    17.         }
    18.        
    19.         private void FixedUpdate()
    20.         {
    21.             playerLocomotion.HandleAllMovement();
    22.         }
    23.    
    24. }
    25.  
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class  InputManager : MonoBehaviour
    4. {
    5.     PlayerControls playerControls;
    6.     AnimatorManager animatorManager;
    7.  
    8.     public Vector2 movementInput;
    9.     private float moveAmount;
    10.     public float verticalInput;
    11.     public float horizontalInput;
    12.  
    13.     private void Awake()
    14.     {
    15.         AnimatorManager animatorManager = GetComponent<AnimatorManager>();
    16.     }
    17.  
    18.     private void OnEnable()
    19.     {
    20.         if (playerControls == null)
    21.         {
    22.             playerControls = new PlayerControls();
    23.  
    24.             playerControls.PlayerMovement.Movement.performed += i => movementInput = i.ReadValue<Vector2>();
    25.         }
    26.      
    27.         playerControls.Enable();
    28.     }
    29.  
    30.  
    31.     private void OnDisable()
    32.     {
    33.         playerControls.Disable();
    34.     }
    35.  
    36.     public void HandleAllInputs()
    37.     {
    38.         HandleMovementInput();
    39.     }
    40.  
    41.     private void HandleMovementInput()
    42.     {
    43.         verticalInput = movementInput.y;
    44.         horizontalInput = movementInput.x;
    45.         moveAmount = Mathf.Clamp01(Mathf.Abs(horizontalInput) + Mathf.Abs(verticalInput));
    46.         animatorManager.UpdateAnimatorValues(0, moveAmount);
    47.     }
    48. }
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,895
    Well the error tells you that your AnimationManager is null. Might want to look at why it's not there.
     
  3. Wilc0

    Wilc0

    Joined:
    Jun 3, 2020
    Posts:
    20
    It is there
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3.  
    4. public class AnimatorManager : MonoBehaviour
    5. {
    6.     Animator animator;
    7.     int horizontal;
    8.     int vertical;
    9.  
    10.     private void Awake()
    11.     {
    12.         animator = GetComponent<Animator>();
    13.         horizontal = Animator.StringToHash("Horizontal");
    14.         vertical = Animator.StringToHash("Vertical");
    15.     }
    16.  
    17.  
    18.     public void UpdateAnimatorValues(float horizontalMovement, float verticalMovement)
    19.     {
    20.         //Animation Snapping
    21.         float snappedHorizontal;
    22.         float snappedVertical;
    23.  
    24.         #region Snapped Horizontal
    25.         if (horizontalMovement > 0 && horizontalMovement < 0.55f)
    26.         {
    27.             snappedHorizontal = 0.5f;
    28.         }
    29.         else if (horizontalMovement > 0.55f)
    30.         {
    31.             snappedHorizontal = 1;
    32.         }
    33.         else if (horizontalMovement < 0 && horizontalMovement > -0.55f)
    34.         {
    35.             snappedHorizontal = -0.5f;
    36.         }
    37.         else if (horizontalMovement < -0.55f)
    38.         {
    39.             snappedHorizontal = -1;
    40.         }
    41.         else
    42.         {
    43.             snappedHorizontal = 0;
    44.         }
    45.         #endregion
    46.         #region Snapped Vertical
    47.         if (verticalMovement > 0 && verticalMovement < 0.55f)
    48.         {
    49.             snappedVertical = 0.5f;
    50.         }
    51.         else if (verticalMovement > 0.55f)
    52.         {
    53.             snappedVertical = 1;
    54.         }
    55.         else if (verticalMovement < 0 && verticalMovement > -0.55f)
    56.         {
    57.             snappedVertical = -0.5f;
    58.         }
    59.         else if (verticalMovement < -0.55f)
    60.         {
    61.             snappedVertical = -1;
    62.         }
    63.         else
    64.         {
    65.             snappedVertical = 0;
    66.         }
    67.         #endregion
    68.        
    69.         animator.SetFloat(horizontal, snappedHorizontal, 0.1f, Time.deltaTime);
    70.         animator.SetFloat(vertical, snappedVertical, 0.1f, Time.deltaTime);
    71.     }
    72. }
    73.  
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Wilc0 likes this.
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,895
    But it's not. Error messages don't lie.
     
  6. Wilc0

    Wilc0

    Joined:
    Jun 3, 2020
    Posts:
    20
    Alright, thanks for your witty quips. Some help would be appreciated identifying it. Ive been staring at it for 3 hours. But ill keep reading docs n tryin to figure it out.
     
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,895
    I already told you the source of your error:
    Have you made sure you attached it to the same game object that your input manager is on?
     
  8. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    Line 15 is your problem in InputManager.
     
    Wilc0 likes this.
  9. Wilc0

    Wilc0

    Joined:
    Jun 3, 2020
    Posts:
    20
    I have, it loads the player and the idle animation, but its not returnin the values for walk and run.
     
  10. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,895
    Actually, good spot mate. OP is re-declaring the field.

    This:
    Code (CSharp):
    1. AnimatorManager animatorManager;
    Has no relation to this:
    Code (CSharp):
    1. AnimatorManager animatorManager = GetComponent<AnimatorManager>();
     
    Wilc0 likes this.
  11. Wilc0

    Wilc0

    Joined:
    Jun 3, 2020
    Posts:
    20
    So, I should get rid of the first declaration?
     
  12. Wilc0

    Wilc0

    Joined:
    Jun 3, 2020
    Posts:
    20
    Or rather, I should get rid of the AnimatorManager animatorManager = GetComponent<AnimatorManager>();
     
  13. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,895
    You want to make sure you're assigning to the correct field, not a transient field.

    So the culprit line should become:
    Code (CSharp):
    1.     private void Awake()
    2.     {
    3.        animatorManager = GetComponent<AnimatorManager>();
    4.     }
    You were re-declaring a field in Awake which has no connection to the field declared in the body of the class.
     
    Wilc0 and RadRedPanda like this.
  14. Wilc0

    Wilc0

    Joined:
    Jun 3, 2020
    Posts:
    20
    Awe, got it. Thank you guys very much for the help. Just had to eliminate the second declaration. That has been drivin me nuts.
     
  15. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    Yeah, C# allows you to declare local variables with the same name as other members, which can cause subtle or not-so-subtle bugs.