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.
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