Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

UnityEvent.AddListener throwing nullReferenceException

Discussion in 'Scripting' started by Gramms66, Aug 27, 2020.

  1. Gramms66

    Gramms66

    Joined:
    Aug 15, 2017
    Posts:
    31
    Hello There!

    I'm trying to implement unity events to my game, but when I add a listener from code I get a nullReferenceException on the AddListener line, for some reason.

    The relevant code
    Code (CSharp):
    1. public class Player : MonoBehaviour
    2. {
    3. private void Start() {
    4.             onRecoil.AddListener(cameraController.ApplyImpulse);
    5. }
    6.  
    7. public class RecoilEvent : UnityEvent<Vector2> {}
    8. public RecoilEvent onRecoil;
    9.  
    10. private void Fire() {
    11.        Vector2 recoil = currentWeapon.Fire();
    12.         if (recoil == -Vector2.one)
    13.             return;
    14.  
    15.         if(onRecoil != null)
    16.             onRecoil.Invoke(recoil);
    17. }
    18. }
    Any help is much appreciated
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    Where are you declaring and assigning the
    cameraController
    variable?
     
  3. Gramms66

    Gramms66

    Joined:
    Aug 15, 2017
    Posts:
    31
    It is declared in the Player class, and assigned in the inspector, I checked it multiple times, it is assigned.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    Debug.Log right before line 4 the values of
    onRecoil
    and
    cameraController
    . One of those two is null:

    Code (CSharp):
    1. Debug.Log($"onRecoil is: {onRecoil}, cameraController is: {cameraController}");
    One possibility also is that you have multiple copies of this script in your scene by accident, one of which is not configured properly.
     
    OlaBrahammar likes this.
  5. Gramms66

    Gramms66

    Joined:
    Aug 15, 2017
    Posts:
    31
    I got back only 1 log entry

    onRecoil is: , cameraController is: CameraControls (CMF.CameraController)
    UnityEngine.Debug:Log(Object)
    FPS.Gameplay.Player:Start() (at Assets/Scripts/Player.cs:28)
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    Based on your log,
    onRecoil
    is null.
     
  7. Gramms66

    Gramms66

    Joined:
    Aug 15, 2017
    Posts:
    31
    That is odd, as you can see in the script I shared, onRecoil is a UnityEvent. I'm new to the UnityEvent system, maybe I'm missing something? Should I assign something to onRecoil?
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    What's showing in the inspector? Try this perhaps?

    Code (CSharp):
    1. public RecoilEvent onRecoil = new RecoilEvent();
     
    restush96 and Gramms66 like this.
  9. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    You might also have to write a zero-arg constructor for RecoilEvent which calls UnityEvent's zero-arg constructor:

    Code (CSharp):
    1. public class RecoilEvent : UnityEvent<Vector2> {
    2.   public RecoilEvent : base() { }
    3. }
     
  10. Gramms66

    Gramms66

    Joined:
    Aug 15, 2017
    Posts:
    31
    Yes thank you, that solved it, I feel so silly...
     
    PraetorBlue likes this.