Object reference not set to an instance of an object

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

PlayerMovement.MovementManagement (Single horizontal, Single vertical, Boolean sneaking) (at Assets/PlayerMovement.cs:52)

PlayerMovement.FixedUpdate () (at Assets/PlayerMovement.cs:33)

tried to make a game of your training videos, but there were errors made ​​at all was said.

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour
{
    public AudioClip shoutingClip;      // Audio clip of the player shouting.
    public float turnSmoothing = 15f;   // A smoothing value for turning the player.
    public float speedDampTime = 0.1f;  // The damping for the speed parameter
    

    private Animator anim;              // Reference to the animator component.
    private HashIDs hash;               // Reference to the HashIDs.
    
    
    void Awake ()
    {
        // Setting up the references.
        anim = GetComponent<Animator>();
        hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<HashIDs>();
        
        // Set the weight of the shouting layer to 1.
        anim.SetLayerWeight(1, 1f);
    }
    
    
    void FixedUpdate ()
    {
        // Cache the inputs.
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        bool sneak = Input.GetButton("Sneak");
        
        MovementManagement(h, v, sneak);
    }
    
    
    void Update ()
    {
        // Cache the attention attracting input.
        bool shout = Input.GetButtonDown("Attract");
        
        // Set the animator shouting parameter.
        anim.SetBool(hash.shoutingBool, shout);
        
        AudioManagement(shout);
    }
    
    
    void MovementManagement (float horizontal, float vertical, bool sneaking)
    {
        // Set the sneaking parameter to the sneak input.
        anim.SetBool(hash.sneakingBool, sneaking);
        
        // If there is some axis input...
        if(horizontal != 0f || vertical != 0f)
        {
            // ... set the players rotation and set the speed parameter to 5.5f.
            Rotating(horizontal, vertical);
            anim.SetFloat(hash.speedFloat, 5.5f, speedDampTime, Time.deltaTime);
        }
        else
            // Otherwise set the speed parameter to 0.
            anim.SetFloat(hash.speedFloat, 0);
    }
    
    
    void Rotating (float horizontal, float vertical)
    {
        // Create a new vector of the horizontal and vertical inputs.
        Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);
        
        // Create a rotation based on this new vector assuming that up is the global y axis.
        Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
        
        // Create a rotation that is an increment closer to the target rotation from the player's rotation.
        Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
        
        // Change the players rotation to this new rotation.
        rigidbody.MoveRotation(newRotation);
    }
    
    
    void AudioManagement (bool shout)
    {
        // If the player is currently in the run state...
        if(anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.locomotionState)
        {
            // ... and if the footsteps are not playing...
            if(!audio.isPlaying)
                // ... play them.
                audio.Play();
        }
        else
            // Otherwise stop the footsteps.
            audio.Stop();
        
        // If the shout input has been pressed...
        if(shout)
            // ... play the shouting clip where we are.
            AudioSource.PlayClipAtPoint(shoutingClip, transform.position);
    }
}

The error message tells you exactly where to look. Reading from the bottom up, the error message tells you that in “FixedUpdate” you are calling something at line 33 that is breaking. Then the other error line tells you that you are actually calling MovementManagement and it breaks at line 52. Then the actual exception says that on line 52, you are accessing some object that doesn’t exist.

On line 52 you access 3 objects hash, sneaking and anim. To figure out which of these doesn’t exist try putting in some debug log statements before line 52.

Debug.Log("sneaking is" + sneaking);
Debug.Log("Hash.sneakingbool is " + hash.sneakingBool);

Presumably one of those two lines will break. Probably the second one.

If it does break on that line, then it would appear that hash isn’t being created correctly.

I believe the error comes from this line:

hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<HashIDs>();

as I’ve never seen the Tags.gameController syntax. Try replacing it with just "gameController" in quotes like this:

hash = GameObject.FindGameObjectWithTag("gameController").GetComponent<HashIDs>();

[Although if I recall correctly, it should also probably be "GameController" with a capital G if you are using the built-in tag]

If none of that works, make sure you actually have an object tagged as GameController in your scene that has a HashIds component

Edit: I’ll leave my previous answer in the below quote as a viable work-around, but having moved on from this point I now realise that the real issue is something to do with the default GameController tag. Add a new tag, call it anything other than GameController (I called mine Fred), and change all references to your GameController tag to Fred (including, obviously, changing the gameController object’s tag to Fred). I have no f**king clue what the underlying cause of this issue is, but hopefully this will fix it.

I know this question is a year old,
but since I’m doing the same tutorial
and have run into the exact same
issue (and been stuck on it for a few
hours), I figured others are probably
struggling with this too.

Since I’m only a beginner at both C#
and Unity, I’m not sure what the most
elegant solution is, but I managed to
make it all work by getting rid of the
HashIDs script altogether. So delete
these lines: private HashIDs hash;
and hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent();

Then, anywhere in your script that
references the now deleted hash
variable, just type that reference
from your HashIDs script directly into
this script.

eg: anim.SetFloat("Speed", 0f);

The conversion (if there even is one?)
happens automatically without the
Animator.StringToHash method. You
will only have to use this once in the
AudioManagement if statement, change
it to this:
if(anim.GetCurrentAnimatorStateInfo(0).nameHash == Animator.StringToHash("Base Layer.Locomotion"))

I get what he was trying to do with
the HashIDs script, but since you
rarely have to type any of these lines
more than once or twice (in any game
perhaps?) it’s not really making
anything more hassle free or
convenient.

Note that the HashIDs script is also
used for enemy animations which I
haven’t covered yet in the tutorial,
so you’ll have to make similar changes
there too.

i think your problem is under gameController object in hierarchy go to secondryMusic and change tag to untagged.

i think your problem is under gameController object in hierarchy go to secondryMusic and change tag to untagged.