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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Can't subscribe listeners to an Event.

Discussion in 'Scripting' started by JustaDuck97, Dec 11, 2020.

  1. JustaDuck97

    JustaDuck97

    Joined:
    Jul 31, 2019
    Posts:
    45
    I have created an event that will run when an interactable object enters the radius of the player, but when attempting to subscribe other scripts to the events I get this error: NullReferenceException: Object reference not set to an instance of an object.

    I thought at first it was a was a script execution order error and the other scripts were calling on the singleton instance before it was set, but when I changed the order to be sure the instance was first establish the error persisted. I found a thread with a similar problem here: https://forum.unity.com/threads/why-can-i-not-subscribe-to-an-event-of-a-singleton.838417/
    but they gave up on trying to find a solution.

    This is my event handler script:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5. //System is require to used GameEvents
    6.  
    7. public class GameEvents : MonoBehaviour
    8. {
    9.     /*
    10.     This is the Custom Event System.
    11.     This Script will contain ways to notify other scripts when an event occours.
    12.     Events like playerdeath, when a player can interact, or the end of a round.
    13.     All objects that react to events will need access to this script.
    14.     There can only be one instance of this object
    15.     This helps reduce the amount of dependancies
    16.     -S
    17.     */
    18.  
    19.     //This is the refference to the script.
    20.     public static GameEvents current;
    21.  
    22.     private void awake()
    23.     {  
    24.         current = this;
    25.      
    26.     }
    27.  
    28.     //This is the Event to allow the player to Interact
    29.     public event Action onInteractionRadiusEnter;
    30.     //This is the Fuction of that Event that notifies the other scripts the radius has been entered
    31.     public void interactionRadiusEnter()
    32.     {  
    33.         if(onInteractionRadiusEnter != null)
    34.         {
    35.             onInteractionRadiusEnter();
    36.             Debug.Log("interaction radius entered");
    37.         }
    38.     }
    39.  
    40.     public event Action onInteractionRadiusExit;
    41.  
    42.     public void interactionRadiusExit()
    43.     {
    44.         if(onInteractionRadiusExit != null)
    45.         {
    46.            onInteractionRadiusExit();
    47.         }
    48.     }
    49. }
    and this is the script I am trying to subscribe to the event:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerInteraction : MonoBehaviour
    6. {
    7.     private bool playerCanInteractWithObjects = false;
    8.     private GameObject objectToInteractWith;
    9.     private Interactable objectInteraction;
    10.  
    11.     private void Start()
    12.     {
    13.       GameEvents.current.onInteractionRadiusEnter += playerCanInteract;
    14.       GameEvents.current.onInteractionRadiusExit += playerCannotInteract;
    15.     }
    16.  
    17.     //This Triggers the enter event;
    18.  
    19.  
    20.  
    21.     public void OnTriggerEnter2D(Collider2D other)
    22.     {
    23.         if (other.gameObject.CompareTag("Interactable"))
    24.         {
    25.             objectToInteractWith = other.gameObject;
    26.             GameEvents.current.interactionRadiusEnter();
    27.             Debug.Log("inteaction raduis entered");
    28.         }
    29.  
    30.     }
    31.  
    32.     //This triggers the exit Event
    33.     public void OnTriggerExit2D(Collider2D other)
    34.     {
    35.         if (other.gameObject.CompareTag("Interactable"))
    36.         {
    37.             objectToInteractWith = null;
    38.             GameEvents.current.interactionRadiusExit();
    39.         }
    40.  
    41.     }

    its worth noting that this error isn't isolated to this script. I've create other scripts just to see if they can subscribe and they cannot. I am completely out of ideas on this one.
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    The singleton instance is null because of this:
    Code (CSharp):
    1.     private void awake()
    2.     {
    3.         current = this;
    4.    
    5.     }
    Awake
    should be spelled with a capital "A".

    On a somewhat unrelated note, you can shorten your event invocations from this...
    Code (CSharp):
    1. if(someEvent != null)
    2. {
    3.    someEvent();
    4. }
    ...To this:
    Code (CSharp):
    1. someEvent?.Invoke();
     
  3. JustaDuck97

    JustaDuck97

    Joined:
    Jul 31, 2019
    Posts:
    45
    god, thats is embarrassing. Thank you so much mate. I spent 2 hours trying to figure this out
     
  4. archentitykalim

    archentitykalim

    Joined:
    May 15, 2019
    Posts:
    6
    Can you please describe how you fixed this? I think I'm having the same problem and I'm too much of a noob to solve it.
     
  5. chrische5

    chrische5

    Joined:
    Oct 12, 2015
    Posts:
    52
    Hello

    He write Awake instead of awake.

    Christoph