Why the Animator variable is all the time null ?

I have a script i created now using enum for playing animations states.
In the Animator window in the editor i created two new states: Walk and Run.

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

public class Animations : MonoBehaviour
{
    public enum AnimatorStates
    {
        WALK, RUN, IDLE
    }

    private Animator _anim;

    void Start()
    {
        _anim = GetComponent<Animator>();
    }

    public void PlayState(AnimatorStates state)
    {
        string animName = string.Empty;
        switch (state)
        {
            case AnimatorStates.WALK:
                animName = "Walk";
                break;
        }
        if (_anim == null)
            _anim = GetComponent<Animator>();

        _anim.Play(animName);
    }
}

I even added two new lines to make sure if it’s null the _anim to geth the component Animator again:

if (_anim == null)
            _anim = GetComponent<Animator>();

But nothing helped so far.
Using a break point first it’s getting to the Start function and after the line

_anim = GetComponent<Animator>();

_anim is NOT null.

But when it’s getting to the part later:

if (_anim == null)
            _anim = GetComponent<Animator>();

        _anim.Play(animName);

This time _anim is null. The exception is on the line

_anim = GetComponent<Animator>();

I can’t figure out why it’s null on the second time. In the Start function it’s not null but in this part it’s null.

And this is how i call the PlayState function.
From another script i’m doing in the top:

private Animations anims;

Then in the Start function

void Start()
    {
        anims = new Animations();
    }

Then in Update function:

void Update()
    {
        if (MyCommands.walkbetweenwaypoints == true)
        {
            anims.PlayState(Animations.AnimatorStates.WALK);
         }
     }

This is where i call the PlayState. And this is where the _anim in the Animations script is null.
After the line in the other script: anims.PlayState(Animations.AnimatorStates.WALK);
It’s going to the Animations script and doing the PlayState part and this time _anim is null.

I can’t figure out why in the PlayState _anim is null.

This is the Exception message i’m getting when running the game and when the condition is true and it’s calling the PlayState:

NullReferenceException
UnityEngine.Component.GetComponent[Animator] () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/ComponentBindings.gen.cs:45)
Animations.PlayState (AnimatorStates state) (at Assets/MyScripts/Animations.cs:29)
WayPoints.Update () (at Assets/MyScripts/WayPoints.cs:55)

Line 29 in Animations is:

Code (csharp):

    • if (_anim == null)
  • _anim = GetComponent();

Line 29 is the _anim = GetComponent(); (it’s the line in the PlayState)

And line 55 in the WayPoints script:

Code (csharp):

    • anims.PlayState(Animations.AnimatorStates.WALK);

Screenshot of the Animator window in the editor:

This is the Exception message i’m getting when running the game and when the condition is true and it’s calling the PlayState:

NullReferenceException
UnityEngine.Component.GetComponent[Animator] () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/ComponentBindings.gen.cs:45)
Animations.PlayState (AnimatorStates state) (at Assets/MyScripts/Animations.cs:29)
WayPoints.Update () (at Assets/MyScripts/WayPoints.cs:55)

Line 29 in Animations is:

if (_anim == null)
            _anim = GetComponent<Animator>();

Line 29 is the _anim = GetComponent(); (it’s the line in the PlayState)

And line 55 in the WayPoints script:

anims.PlayState(Animations.AnimatorStates.WALK);

Animations is a MonoBehaviour, you should not be creating MonoBehaviours with the new keyword. Use AddComponent / GetComponent instead.

1 Like

Having same problem. I’m not creating animator component in runtime, just trying to get reference to the existing one.

UPD: half an hour later, it appeared to be that for some weird reason prefab was not saving itself with “Apply” button, and started to work only when I saved the scene (though components and references are internal for prefab, i.e. all inside and none outside)