Another Null Reference Error Problem

Okay, I’ve been trying to fix this issue on my project for around 3 hours now with no luck, and I’ve followed and applied other peoples solutions from the last few lectures, as well as change my code base on several occasions, but with no luck.

The issue occurs when clicking anywhere after the game starts, whether on a potential target or just on a walkable area, and displays the 2 null reference errors for both the Fighter and Mover scripts depending on what I’ve selected.

I’m using Unity ver 2020.3.10f1, and here is the current code I have for Fighter and Mover

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

namespace RPG.Combat
{
    public class Fighter : MonoBehaviour, IAction
    {
        [SerializeField] float weaponRange = 2f;
        Transform target;

        void Update()
        {
            if (target == null) return; //stops Mover.Stop() from being returned each frame if we're not in combat

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

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

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

        public void Cancel()
        {
            target = null;
        }
    }
}
Mover
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using RPG.Core;

namespace RPG.Movement
{
    public class Mover : MonoBehaviour, IAction
    {
        [SerializeField] Transform target;

        NavMeshAgent navMeshAgent;

        void Start()
        {
            navMeshAgent = GetComponent<NavMeshAgent>();
        }

        void Update()
        {
            UpdateAnimator();
        }

        public void StartMoveAction(Vector3 destination)
        {
            GetComponent<ActionScheduler>().StartAction(this);
            MoveTo(destination);
        }

        public void MoveTo(Vector3 destination)
        {
            navMeshAgent.destination = destination;
            navMeshAgent.isStopped = false;
        }

        public void Cancel()
        {
            navMeshAgent.isStopped = true;    
        }

        private void UpdateAnimator()
        {
            Vector3 velocity = navMeshAgent.velocity;
            Vector3 localVelocity = transform.InverseTransformDirection(velocity);
            float speed = localVelocity.z;
            GetComponent<Animator>().SetFloat("forwardSpeed", speed);
        }
  
    }
}
1 Like

Argh… I can see from the image what the problem was. I haven’t added ‘ActionScheduler’ script as a component for the Player. :man_facepalming:

Tested and it works.

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

Privacy & Terms