Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Bug Problem with the Animator

Discussion in 'Editor & General Support' started by Roman50060, Mar 10, 2024.

  1. Roman50060

    Roman50060

    Joined:
    Feb 28, 2024
    Posts:
    3
    Hello Friends...

    I am currently in the tutorial of Code Monkey and now I have a problem. Yesterday I closed Unity and everything was still okay. Today I start Unity and have this error.

    NullReferenceException: Object reference not set to an instance of an object
    PlayerAnimator.Update () (at Assets/Scripts/PlayerAnimator.cs:21)

    Whenever I add this code

    animator.SetBool(IS_WALKING, player.IsWalking());

    I hope someone can help me here.

    Here is the Code that the Error Shows me

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerAnimator : MonoBehaviour
    {
    private const string IS_WALKING = "IsWalking";

    [SerializeField] private Player player;

    private Animator animator;

    private void Awake()
    {
    animator = GetComponent<Animator>();
    }

    private void Update()
    {
    animator.SetBool(IS_WALKING, player.IsWalking());
    }

    }
     

    Attached Files:

    Last edited: Mar 10, 2024
  2. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    2,256
    Dont post pictures of code

    but clearly you arent understanding what codemonkey has said cos that script requires a player, and an animator.. You didnt fill in player and you removed the animator.. how on earth did you expect it to work
     
  3. Roman50060

    Roman50060

    Joined:
    Feb 28, 2024
    Posts:
    3
    Hay Bugfinder, i will remove the Picture of the code.

    I have the Animator, and the Player. The Player also moves and the Head Animation Works. Only the Walking Animation wont Work. Everything is okay with the Logic and Code so far only this Error is stopping me.
     

    Attached Files:

  4. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    2,256
    no code tags are as bad as pictures :p

    anyway you havent grasped the point of this code, go back and watch the tutorial again.



    clearly shows
    YOU have not assigned the player, it says none
    YOU removed the animator, which it looks for in the code to do stuff..
    ergo, it will not work

    Plus, debug and prove to yourself what it says is null.
     
  5. Roman50060

    Roman50060

    Joined:
    Feb 28, 2024
    Posts:
    3
    Hey BugFinder

    Thank you very much for you help, i dont know how i could miss that part. It works now. Thank you very much!