NullReferenceException

Hi, i was programming a 2D game with Unity when this error appeared: NullReferenceException: Object reference not set to an instance of an object Agent.Update () (at Assets/Agent.cs:26); and i can’t solve this error, can someone help me please?

Here the code:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class Agent : MonoBehaviour
{
    private AgentAnimations agentAnimations;
    private AgentMover agentMover;

    private WeaponParent weaponParent;


    private Vector2 pointerInput, movementInput;

    public Vector2 PointerInput { get => pointerInput; set => pointerInput = value; }
    public Vector2 MovementInput { get => movementInput; set => movementInput = value; }

    private void Update()
    {
        //pointerInput = GetPointerInput();
        //MovementInput = movement.action.ReadValue<Vector2>().normalized;

        agentMover.MovementInput = MovementInput;
        weaponParent.PointerPosition = pointerInput;
        AnimateCharacter();
    }





    public void PerformAttack()
    {
        weaponParent.Attack();
    }

    private void Awake()
    {
        agentAnimations = GetComponentInChildren<AgentAnimations>();
        weaponParent = GetComponentInChildren<WeaponParent>();
        agentMover = GetComponent<AgentMover>();
    }

    private void AnimateCharacter()
    {
        Vector2 lookDirection = pointerInput - (Vector2)transform.position;
        agentAnimations.RotateToPointer(lookDirection);
        agentAnimations.PlayAnimation(MovementInput);
    }
}

You need to assign a value to weaponParent. The variable is private, not serialized, and never assigned, therefore it is guaranteed to be null. You need to either:

  • Assign it in code somewhere by doing weaponParent = <some valid reference>;
  • Mark it as public or with [SerializeField] and drag & drop to assign in the inspector.
1 Like

And for future reference, you never have to post in the forums for a null reference.

Why? Because it isn’t helpful. There are only THREE steps, and they’re ALWAYS the same.

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null
  • Identify why it is null
  • Fix that

I tried it, but it continues to give me the error

Show what you tried - you must have done something incorrectly.

Stop, start with the first step. There’s only three steps.

You are of course welcome to follow any other steps you like.

When you are done following other steps, the ONLY three steps that will solve your problem are waiting patiently above for you to begin.

I did this:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Agent : MonoBehaviour
{
    public weaponParent = <some valid reference>
    private AgentAnimations agentAnimations;
    private AgentMover agentMover;
    private WeaponParent weaponParent;
    private Vector2 pointerInput, movementInput;
    public Vector2 PointerInput { get => pointerInput; set => pointerInput = value; }
    public Vector2 MovementInput { get => movementInput; set => movementInput = value; }
    private void Update()
    {
        //pointerInput = GetPointerInput();
        //MovementInput = movement.action.ReadValue<Vector2>().normalized;
        agentMover.MovementInput = MovementInput;
        weaponParent.PointerPosition = pointerInput;
        AnimateCharacter();
    }
    public void PerformAttack()
    {
        weaponParent.Attack();
    }
    private void Awake()
    {
        agentAnimations = GetComponentInChildren<AgentAnimations>();
        weaponParent = GetComponentInChildren<WeaponParent>();
        agentMover = GetComponent<AgentMover>();
    }
    private void AnimateCharacter()
    {
        Vector2 lookDirection = pointerInput - (Vector2)transform.position;
        agentAnimations.RotateToPointer(lookDirection);
        agentAnimations.PlayAnimation(MovementInput);
    }
}
Yesterday at 5:26 PMEditReportReply
PraetorBlue
PraetorBlue
You need to assign a value to weaponParent. The variable is private, not serialized, and never assigned, therefore it is guaranteed to be null. You n

Please use some critical thinking. That was not literal code you copy into your program. That was an example showing you what to do.

I recommend some tutorials to learn the basics first so you understand the building blocks to do this more complicated thing you’re trying to do.
C# Collections | Microsoft Learn
Unity Roll-a-Ball - Unity Learn

I see you are still resisting doing the three steps, as I noted above.

Like I said, those three steps will wait FOREVER for you to do them.

They are the only things that will fix your problem.

I urge you to proceed to them, or as Praetor suggests, move onto some basic tutorials.

We cannot reach through the computer to magically “learn you” things.

We can only suggest approaches that have been successful with every other software engineer in the world.

The problem is that I follow your three tips but if I don’t know how to solve a null, they are useless

The steps detail how to “solve a null”.

It appears that weaponParent is null. Place a Debug.Log statement right after this line to confirm:

weaponParent = GetComponentInChildren<WeaponParent>();
if (weaponParent == null)
  Debug.Log("It's null");
else
  Debug.Log("Not null");

thanks, now it tells me it’s null

So you’ve done Step 1! Now find out why, Step 2. Not sure, but it looks like you are trying to find a parent by looking in children.

1 Like