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

"null" while passing UnityAction as parameter

Discussion in 'Scripting' started by Micio_del_Cheshire, Sep 27, 2018.

  1. Micio_del_Cheshire

    Micio_del_Cheshire

    Joined:
    Oct 24, 2013
    Posts:
    28
    Hello good sirs.
    I'm not very practical about Event and Actions and I was trying to get something out of it, but I'm having an issue.
    I simplified the code to focus on the error. I get an "NullReferenceException: Object reference not set to an instance of an object" on the "addListener" line of code.

    Code (CSharp):
    1. public class A : MonoBehaviour
    2. {
    3.     B instance;
    4.  
    5.     private void Start()
    6.     {
    7.         instance = new B(Function);
    8.     }
    9.  
    10.     public void Function()
    11.     {
    12.         /* stuff  */
    13.     }
    14. }
    15.  
    16. public class B
    17. {
    18.     private UnityEvent theEvent;
    19.  
    20.     public B(UnityAction theAction)
    21.     {
    22.         this.theEvent.AddListener(theAction);
    23.     }
    24. }

    What am I doing wrong?
    Thanks for the answers ^^
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    theEvent is not assigned anywhere. You need to initialize it with new keyword.
    Although, you need to inherit it first I think, otherwise Unity will not let it do so.
    Code (CSharp):
    1. public class YourEvent : UnityEvent{
    2. }
    Then:
    Code (CSharp):
    1. private YourEvent theEvent = new YourEvent();
     
    Micio_del_Cheshire likes this.
  3. Micio_del_Cheshire

    Micio_del_Cheshire

    Joined:
    Oct 24, 2013
    Posts:
    28
    Aw god, you're right. I feel dumb, now XD
    I tested with a Serializated Event in the inspector and now I totally forgot to instatiate it.
    I owe you a beer!
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    Nope, that's wrong. You can just do:

    Code (csharp):
    1. private UnityEvent theEvent;
    There's a couple of things to note, though.
    1: Unless you're persisting these events (ie. you want theEvent to draw in the inspector), there's no reason to use UnityEvent/UnityAction. You can just use the built-in delegates. Those are way faster than UnityEvents, and have a much more comfortable syntax to work with, so the only reason to ever use UnityEvent is if you want to assign things to it in the inspector.

    Here's the same stuff, using Action, which is just a wrapper for a function with no arguments or return values:

    Code (csharp):
    1. public class A : MonoBehaviour
    2. {
    3.     B instance;
    4.  
    5.     private void Start()
    6.     {
    7.         instance = new B(Function);
    8.     }
    9.  
    10.     public void Function()
    11.     {
    12.         /* stuff  */
    13.     }
    14. }
    15.  
    16. public class B
    17. {
    18.     private Action theEvent;
    19.  
    20.     public B(Action theAction)
    21.     {
    22.         theEvent += theAction;
    23.     }
    24. }
     
    Micio_del_Cheshire and xVergilx like this.