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

Whenever I start the game a bunch of errors start to pile up saying something is null.

But I copied the github code line for line so I don’t see how there could be an error. When I double click on the error it takes me to the fighter.cs file. Apparently, line 15 is where the error is but I can’t figure it out. it’s probably something small but I’d really appreciate some help.

The problem here is that we’re checking is in range on an item that can be null (target). If the target is null, we really want the Update() method to do nothing at all.

Put this as the first line in Update and the error message will go away:

if(target==null) return;

The error went away ! but now the weapon range isn’t working. I know the player is supposed to say a few feet away from the enemy when attacking but now its walking directly into the enemy

The first thing to check is to see what the serialized value of the weaponRange variable is in the inspector. If you changed it in the inspector it will always override whatever you put in the script.

If that’s still 2, then we’ll need to take a look at you Mover script. Paste in the text of the script, then select the text you just pasted in and press the </> button.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.AI;

namespace RPG.Movement

{

    public class Mover : MonoBehaviour

{

    NavMeshAgent navMeshAgent;

    [SerializeField] Transform target;

    private void Start()

    {

        navMeshAgent = GetComponent<NavMeshAgent>();

    }

    void Update()

    {

        UpdateAnimator();

    }

    private void UpdateAnimator()

    {

        Vector3 velocity = navMeshAgent.velocity;

        Vector3 localVelocity = transform.InverseTransformDirection(velocity);

        float speed = localVelocity.z;

        GetComponent<Animator>().SetFloat("forwardSpeed", speed);

    }

    public void MoveTo(Vector3 destination)

    {

        GetComponent<NavMeshAgent>().destination = destination;

        navMeshAgent.isStopped = false;

    }

   

    public void Stop()

    {

        navMeshAgent.isStopped = true;

    }

}

}

Just spotted the error, it’s in the Fighter component.
In the if statement to determine if the character is in range, you’ve got
if(isInRange) instead of if(!isInRange). This means that as long as the player is closer to the target it will move instead of the other way round.

I just changed it, and I’m still having the problem. Sorry for being difficult lol. It’s just hard to understand this.

Paste in a screenshot of the Fighter component in the inspector.

like this ?

Let’s alter Fighter.cs with some debugs:

void Update()
{
    if(!target) return;
    bool isInRange = Vector3.Distance(transform.position, target.position) < weaponRange;
    Debug.Log($"{Vector3.Distance(transform.position, target.position)}<{weaponRange} = {isInRange}");
    if(!isInRange)
    {
          Debug.Log("Moving to attack position");
          GetComponent<Mover>().MoveTo(target.position);
     }
     else
     {
          Debug.Log("Take that you cretin!");
          GetComponent<Mover>().Stop();
     }
}

Then uncheck Collapse in the console and hit play. Post a screenshot of the console’s message window.

I did exactly what you said but nothing popped up in the console.

So the player isn’t moving at all?

Paste in your PlayerController.cs (please paste the text of the script, not a screenshot, like you did with mover. Screenshots are difficult to read/diagnose).

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.AI;

using RPG.Combat;

using RPG.Movement;

namespace RPG.Control

{

    public class PlayerController : MonoBehaviour

{

    // Update is called once per frame

    void Update()

    {

        if(InteractWithMovement()) return;

        if(InteractWithCombat()) return;

    }

    private bool InteractWithCombat()

    {

        RaycastHit[] hits = Physics.RaycastAll(GetMouseRay());

        foreach(RaycastHit hit in hits)

        {

            CombatTarget target = hit.transform.GetComponent<CombatTarget>();

            if( target = null) continue;

            if(Input.GetMouseButtonDown(0))

            {

                GetComponent<Fighter>().Attack(target);

            }

            return true;

        }

        return false;

    }

    public bool InteractWithMovement()

    {

        RaycastHit hit;

        bool hasHit = Physics.Raycast(GetMouseRay(), out hit);

        if(hasHit)

        {

            if(Input.GetMouseButton(0))

            {

              GetComponent<Mover>().MoveTo(hit.point);

            }

            return true;

        }

        return false;

    }

    private static Ray GetMouseRay()

    {

        return Camera.main.ScreenPointToRay(Input.mousePosition);

    }

}

}

And to answer your last question, the player is moving, it’s just nothing pops up in the console.

Does your enemy have a CombatTarget?

Yup

I’m not sure what’s going on…
Zip up your project and upload it to https://gdev.tv/projectupload and I’ll take a look at it after work. Be sure to remove the Library folder to save space.

How do I zip up my project ?

https://openclassrooms.zendesk.com/hc/en-us/articles/360004177398-How-do-I-make-a-zip-file-#:~:text=Press%20and%20hold%20(or%20right,created%20in%20the%20same%20location.

Ok I uploaded it… So now what ? just wait ?

Privacy & Terms