[Solved] NullReference after completing EnemyAI (Section 5 Lecture 45)

After completing the lecture, I am getting the following error when the player gets into range of an enemy:

The enemy doesn’t even try to move towards the player. Once the player is out of range of the enemy, the errors stop.

Below is my code for the AIController and Fighter script:

using UnityEngine;
using RPG.Combat;

namespace RPG.Control
{
    public class AIController : MonoBehaviour
    {
        [SerializeField] float chaseDistance = 5f;

        Fighter fighter;
        GameObject player;

        private void Start()
        {
            fighter = GetComponent<Fighter>();
            player = GameObject.FindWithTag("Player");
        }

        private void Update()
        {
            if (InAttackRangeOfPlayer() && fighter.CanAttack(player))
            {
                fighter.Attack(player);
            }
            else
            {
                fighter.Cancel();
            }
        }

        private bool InAttackRangeOfPlayer()
        {
            float distanceToPlayer =  Vector3.Distance(player.transform.position, transform.position);
            return distanceToPlayer < chaseDistance;
        }
    }
}

using UnityEngine;
using RPG.Movement;
using RPG.Core;

namespace RPG.Combat
{
    public class Fighter : MonoBehaviour, IAction
    {

        [SerializeField] float weaponRange = 2f;
        [SerializeField] float timeBetweenAttacks = 1f;
        [SerializeField] float weaponDamage = 10f;

        Health target;
        float timeSinceLastAttack = 0f;

        private void Update()
        {
            timeSinceLastAttack += Time.deltaTime;

            if(target == null)
            {
                return;
            }

            if (target.IsDead())
            {
                return;
            }

            if (!GetIsInRange())
            {
                GetComponent<Mover>().MoveTo(target.transform.position);
            }
            else
            {
                GetComponent<Mover>().Cancel();
                AttackBehaviour();
            }
        }

        private void AttackBehaviour()
        {
            transform.LookAt(target.transform);

            if(timeSinceLastAttack > timeBetweenAttacks)
            {
                // This will trigger the Hit() event.
                TriggerAttack();
                timeSinceLastAttack = 0f;
            }

        }

        private void TriggerAttack()
        {
            GetComponent<Animator>().ResetTrigger("stopAttack");
            GetComponent<Animator>().SetTrigger("attack");
        }

        //Animation Event
        void Hit()
        {
            if(target == null)
            {
                return;
            }
            target.TakeDamage(weaponDamage);
        }

        private bool GetIsInRange()
        {
            return Vector3.Distance(transform.position, target.transform.position) < weaponRange;
        }

        public bool CanAttack(GameObject combatTarget)
        {
            if(combatTarget == null)
            {
                return false;
            }
            Health targetToTest = combatTarget.GetComponent<Health>();
            return targetToTest != null && !targetToTest.IsDead();
        }

        public void Attack(GameObject combatTarget)
        {
            GetComponent<ActionScheduler>().StartAction(this);
            target = combatTarget.GetComponent<Health>();
        }

        public void Cancel()
        {
            StopAttack();
            target = null;
        }

        private void StopAttack()
        {
            GetComponent<Animator>().ResetTrigger("attack");
            GetComponent<Animator>().SetTrigger("stopAttack");
        }
    }
}

The “Player” tag has also been applied to player:

Any help would be much appreciated.

Replied to this in the Udemy Q&A but thanks for keeping it tidy on the forums and using a link.

I suspect this is the “attack” trigger meaning to be “Attack”

I checked the spelling of the trigger and it looks ok. If it was a spelling error, wouldn’t the player not be able to attack? Also posted a short video.

https://community.gamedev.tv/uploads/short-url/bem1YSxbOhHtPukgJhPwvyhddeL.mp4

Okay well this is now a different error line this time

Its saying it cant find the action scheduler. You need to test out why this is happening but i suspect that you havent applied the overrides to the prefab as you were working on the instance.
This should not be the issue but should give you a thread to pull on.

Thank you good sir. That was the perfect thread to pull on. The issue was that the Enemy prefab didn’t have the Action Scheduler script added.

1 Like

Awesome :slight_smile: Just like British Rail, We get there in the end.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms