Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question NullReferenceException

Discussion in 'Scripting' started by GameDev_A, Sep 26, 2022.

  1. GameDev_A

    GameDev_A

    Joined:
    Sep 5, 2022
    Posts:
    43
    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:

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.InputSystem;
    6.  
    7. public class Agent : MonoBehaviour
    8. {
    9.     private AgentAnimations agentAnimations;
    10.     private AgentMover agentMover;
    11.  
    12.     private WeaponParent weaponParent;
    13.  
    14.  
    15.     private Vector2 pointerInput, movementInput;
    16.  
    17.     public Vector2 PointerInput { get => pointerInput; set => pointerInput = value; }
    18.     public Vector2 MovementInput { get => movementInput; set => movementInput = value; }
    19.  
    20.     private void Update()
    21.     {
    22.         //pointerInput = GetPointerInput();
    23.         //MovementInput = movement.action.ReadValue<Vector2>().normalized;
    24.  
    25.         agentMover.MovementInput = MovementInput;
    26.         weaponParent.PointerPosition = pointerInput;
    27.         AnimateCharacter();
    28.     }
    29.  
    30.  
    31.  
    32.  
    33.  
    34.     public void PerformAttack()
    35.     {
    36.         weaponParent.Attack();
    37.     }
    38.  
    39.     private void Awake()
    40.     {
    41.         agentAnimations = GetComponentInChildren<AgentAnimations>();
    42.         weaponParent = GetComponentInChildren<WeaponParent>();
    43.         agentMover = GetComponent<AgentMover>();
    44.     }
    45.  
    46.     private void AnimateCharacter()
    47.     {
    48.         Vector2 lookDirection = pointerInput - (Vector2)transform.position;
    49.         agentAnimations.RotateToPointer(lookDirection);
    50.         agentAnimations.PlayAnimation(MovementInput);
    51.     }
    52. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    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.
     
    Kurt-Dekker likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
  4. GameDev_A

    GameDev_A

    Joined:
    Sep 5, 2022
    Posts:
    43
    I tried it, but it continues to give me the error
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    Show what you tried - you must have done something incorrectly.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    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.
     
  7. GameDev_A

    GameDev_A

    Joined:
    Sep 5, 2022
    Posts:
    43
    I did this:

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.InputSystem;
    6. public class Agent : MonoBehaviour
    7. {
    8.     public weaponParent = <some valid reference>
    9.     private AgentAnimations agentAnimations;
    10.     private AgentMover agentMover;
    11.     private WeaponParent weaponParent;
    12.     private Vector2 pointerInput, movementInput;
    13.     public Vector2 PointerInput { get => pointerInput; set => pointerInput = value; }
    14.     public Vector2 MovementInput { get => movementInput; set => movementInput = value; }
    15.     private void Update()
    16.     {
    17.         //pointerInput = GetPointerInput();
    18.         //MovementInput = movement.action.ReadValue<Vector2>().normalized;
    19.         agentMover.MovementInput = MovementInput;
    20.         weaponParent.PointerPosition = pointerInput;
    21.         AnimateCharacter();
    22.     }
    23.     public void PerformAttack()
    24.     {
    25.         weaponParent.Attack();
    26.     }
    27.     private void Awake()
    28.     {
    29.         agentAnimations = GetComponentInChildren<AgentAnimations>();
    30.         weaponParent = GetComponentInChildren<WeaponParent>();
    31.         agentMover = GetComponent<AgentMover>();
    32.     }
    33.     private void AnimateCharacter()
    34.     {
    35.         Vector2 lookDirection = pointerInput - (Vector2)transform.position;
    36.         agentAnimations.RotateToPointer(lookDirection);
    37.         agentAnimations.PlayAnimation(MovementInput);
    38.     }
    39. }
    40. Yesterday at 5:26 PMEditReportReply
    41. PraetorBlue
    42. PraetorBlue
    43. 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
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    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.
     
  10. GameDev_A

    GameDev_A

    Joined:
    Sep 5, 2022
    Posts:
    43
    The problem is that I follow your three tips but if I don't know how to solve a null, they are useless
     
  11. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    The steps detail how to "solve a null".
     
  12. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    It appears that weaponParent is null. Place a Debug.Log statement right after this line to confirm:

    Code (CSharp):
    1. weaponParent = GetComponentInChildren<WeaponParent>();
    2. if (weaponParent == null)
    3.   Debug.Log("It's null");
    4. else
    5.   Debug.Log("Not null");
     
  13. GameDev_A

    GameDev_A

    Joined:
    Sep 5, 2022
    Posts:
    43
    thanks, now it tells me it's null
     
  14. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    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.
     
    Kurt-Dekker likes this.