FindGameObjectWithTag seemingly not working for my C# Unity Script

I am trying to get my script, which is used for my enemy AI, to find my players transform but I keep getting the error:

NullReferenceException: Object reference not set to an instance of an object
AiChasePlayerState.Update (AiAgent agent) (at Assets/AiChasePlayerState.cs:36)
AiStateMachine.Update () (at Assets/AiStateMachine.cs:33)
AiAgent.Update () (at Assets/AiAgent.cs:25)

The following is my script:

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

public class AiChasePlayerState : AiState
{
    public Transform playerTransform;
    float timer = 0.0f;
    Animator animator;
    
    public AiStateId GetId()
    {
        return AiStateId.ChasePlayer;
    }
    public void Enter(AiAgent agent)
    {
        if (playerTransform == null)
        {
            playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
        }
    }
    public void Update(AiAgent agent)
    {
        timer -= Time.deltaTime;
        if (timer < 0.0f)
        {
            float sqDistance = (playerTransform.position - agent.navMeshAgent.destination).sqrMagnitude;
            if (sqDistance > agent.config.maxDistance * agent.config.maxDistance)
            {
                agent.navMeshAgent.destination = playerTransform.position;
            }
            timer = agent.config.maxTime;
        }
        agent.navMeshAgent.destination = playerTransform.position;
        animator.SetFloat("Speed", agent.navMeshAgent.velocity.magnitude);
    }
    public void Exit(AiAgent agent)
    {

    }
}

I have tagged my player with the tag "Player" so I don’t know why this is happening. Please help me, thank you.


I tried following the Youtube video:

but I got lost because the guy updated his code without addressing it.

Have the game manager (or any other scipt) keep track of the player’s position using a direct reference, make the manager tell other scripts (the AI) the position and have a script on the AI calculate the position stuff for themselves.

Pseudo code:

Manager Script:

// This Manager's knowledge of the player's Vector3 position
private Transform playerPosition;

public GetPlayerPosition()
{
    // Anyone who calls this function will receive the player's position
    return playerPosition.position;
}

If you need a reference to the gameobject, you can do it like this:

// This Manager's knowledge of the player's Vector3 position
private GameObject _PlayerPrefab;

public GetPlayerPosition()
{
    // Anyone who calls this function will receive the player's position
    return _PlayerPrefab.transform.position;
}

AI Script:

// Reference to the manager script
[SerializeField]
private ManagerScript _ManagerScript;

// This AI's knowledge of the player's Vector3 position
private Vector3 playerPosition;

void TrackPlayer()
{
    // Get the player's position
    this.playerPosition = _ManagerScript.GetPlayerPosition;

    // Now you can do even more stuff like checking proximity
    float distanceToPlayer = (playerPosition - this.transform.position).magnitude;

    if (distanceToPlayer < 10f) // When distance is smaller than 10 units
    {
        // Do something very smart here
    }
}

Also I’m sorry if I’ve included some minor syntax errors or stuff, I have not checked this in unity.