Search Unity

Question Doing animations for third person controller

Discussion in 'Scripting' started by Kokes0420, Jun 19, 2021.

  1. Kokes0420

    Kokes0420

    Joined:
    Jun 17, 2021
    Posts:
    2
    when i run the game it writes this error:
    NullReferenceException: Object reference not set to an instance of an object
    InputManager.HandleMovement () (at Assets/game/Scripts/InputManager.cs:47)
    InputManager.HandleAllInputs () (at Assets/game/Scripts/InputManager.cs:39)
    PlayerManager.Update () (at Assets/game/Scripts/PlayerManager.cs:18)

    the game runs but the animations dont change, always in idle anim.

    the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AnimatorManager : MonoBehaviour
    6. {
    7.     Animator animator;
    8.     int horizontal;
    9.     int vertical;
    10.  
    11.     private void Awake()
    12.     {
    13.         animator = GetComponent<Animator>();
    14.         horizontal = Animator.StringToHash("Horizontal");
    15.         vertical = Animator.StringToHash("Vertical");
    16.     }
    17.  
    18.     public void UpdateAnimatorValues(float horizontalMovement, float verticalMovement)
    19.     {
    20.         float snappedHorizontal;
    21.         float snappedVertical;
    22.  
    23.         #region snappedHorizontal
    24.         if (horizontalMovement > 0 && horizontalMovement < 0.55f)
    25.         {
    26.             snappedHorizontal = 0.5f;
    27.         }
    28.         else if (horizontalMovement > 0.55f)
    29.         {
    30.             snappedHorizontal = 1;
    31.         }
    32.         else if (horizontalMovement < 0 && horizontalMovement > -0.55f)
    33.         {
    34.             snappedHorizontal = -0.5f;
    35.         }
    36.         else if (horizontalMovement < -0.55f)
    37.         {
    38.             snappedHorizontal = -1;
    39.         }
    40.         else
    41.         {
    42.             snappedHorizontal = 0;
    43.         }
    44.         #endregion
    45.  
    46.         #region snappedVertical
    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.5f)
    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.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class InputManager : MonoBehaviour
    6. {
    7.     PlayerControlls playerControlls;
    8.     AnimatorManager animatorManager;
    9.  
    10.     public Vector2 movementInput;
    11.     private float moveAmount;
    12.     public float verticalInput;
    13.     public float horizontalInput;
    14.  
    15.     private void Awake()
    16.     {
    17.         animatorManager = GetComponent<AnimatorManager>();
    18.     }
    19.  
    20.     private void OnEnable()
    21.     {
    22.         if(playerControlls == null)
    23.         {
    24.             playerControlls = new PlayerControlls();
    25.  
    26.             playerControlls.PlayerMovement.Movement.performed += i => movementInput = i.ReadValue<Vector2>();
    27.         }
    28.  
    29.         playerControlls.Enable();
    30.     }
    31.  
    32.     private void OnDisable()
    33.     {
    34.         playerControlls.Disable();
    35.     }
    36.  
    37.     public void HandleAllInputs()
    38.     {
    39.         HandleMovementInput();
    40.     }
    41.  
    42.     private void HandleMovementInput()
    43.     {
    44.         verticalInput = movementInput.y;
    45.         horizontalInput = movementInput.x;
    46.         moveAmount = Mathf.Clamp01(Mathf.Abs(horizontalInput) + Mathf.Abs(verticalInput));
    47.         animatorManager.UpdateAnimatorValues(0, moveAmount);
    48.     }
    49. }
    50.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerManager : MonoBehaviour
    6. {
    7.     InputManager inputManager;
    8.     Playerlocomotion playerLocomotion;
    9.  
    10.     private void Awake()
    11.     {
    12.         inputManager = GetComponent<InputManager>();
    13.         playerLocomotion = GetComponent<Playerlocomotion>();
    14.     }
    15.  
    16.     private void Update()
    17.     {
    18.         inputManager.HandleAllInputs();
    19.     }
    20.  
    21.     private void FixedUpdate()
    22.     {
    23.         playerLocomotion.HandleAllMovement();
    24.     }
    25. }
    26.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Fortunately it is completely irrelevant what you are trying to do. Why?

    Because the answer is always the same for null reference... ALWAYS. It is the single most common error ever.

    Don't waste your life spinning around and round on this error. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception
    - also known as: Object reference not set to an instance of an object

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.

    Here is a clean analogy of the actual underlying problem of a null reference exception:

    https://forum.unity.com/threads/nul...n-instance-of-an-object.1108865/#post-7137032
     
  3. Kokes0420

    Kokes0420

    Joined:
    Jun 17, 2021
    Posts:
    2
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    In order to solve this problem you will need to understand the three steps above.

    You have posted scripts for THREE (3) different manager classes. Right away that is WAAAAAAAY too much going on for a first tutorial. No wonder you're struggling.

    You might want to back up and start with some simpler tutorials, such as these:

    Imphenzia / imphenzia - super-basic Unity tutorial:




    Brackeys super-basic Unity Tutorial series:



    Sebastian Lague Intro to Game Development with Unity and C#:



    How to do tutorials properly:

    Tutorials are a GREAT idea. Tutorials should be used this way:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation generally ends in disaster. That's how software engineering works. Every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly. Fortunately this is the easiest part to get right. Be a robot. Don't make any mistakes. BE PERFECT IN EVERYTHING YOU DO HERE.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!
     
    Kokes0420 likes this.