Search Unity

Null reference error with an event

Discussion in 'Scripting' started by kotoffei, Apr 22, 2021.

  1. kotoffei

    kotoffei

    Joined:
    Jul 8, 2019
    Posts:
    15
    hello everyone!

    I'm trying to use an event to launch an ability.

    So, my event is in player controller. I want to use a key to "Initialize" an ability. My ability is a SO which attach a monobehaviour to the player.
    The event is colled from ennemy script when i'm clicking on it.

    But when i use a key to initialize my ability i have a null reference error.

    So, my code:
    PlayerController.Cs

    Code (CSharp):
    1. public event Action<GameObject> UseAbility;
    2. public void LaunchAbility(GameObject target)
    3.         {
    4.          
    5.          
    6.                 Debug.Log("in player controller the target  is now  " + target.name);
    7.                 UseAbility(target);
    8.              
    9.              
    10.         }
    SpecialAbilities.CS
    Code (CSharp):
    1. private void CheckForAbility()
    2.         {
    3.             if (Input.GetKeyDown(KeyCode.Alpha1))//keyIndex.ToString())
    4.             {
    5.                 abilities[0].SetUp();
    6.                
    7.                
    8.             }
    9.         }
    AbilitySO.cs
    Code (CSharp):
    1. public void SetUp()
    2.         {
    3. //behaviour is the monobehaviour script of the ability on the player
    4.             behaviour.Setup();
    5.         }
    AbilityBehaviour.cs
    Code (CSharp):
    1. public override void Setup()
    2.         {
    3.             playerController.UseAbility += TriggerAbility;
    4.         }
    5. public override void TriggerAbility(GameObject target)
    6.         {
    7.             GetComponent<Fighter>().Attack(target);
    8.            
    9.         }
    Enemy.cs
    Code (CSharp):
    1. if (Input.GetMouseButton(1))
    2.             {
    3.                playerController.LaunchAbility(gameObject);}
    4.  
    When i try to Setup an ability i have a null reference error. Why i have this error? If there is better logic, i would also be greatfull.

    Thank you in advance.
     
  2. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    677
    You'll need to post more complete code for us to help you. Too much is missing from this, I think. For example, at Line 4 in AbilitySO.cs, what, if anything, has assigned a reference to the
    behaviour
    variable?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    There's no need to make a forum post or post any code for a null reference error.

    Why?

    Because the answer is always the same... ALWAYS. It is the single most common error ever.

    Don't waste your life spinning around and round on this error. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception
    - also known as: Object reference not set to an instance of an object

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.
     
    kotoffei likes this.
  4. kotoffei

    kotoffei

    Joined:
    Jul 8, 2019
    Posts:
    15
    Thank you very much. I managed to fix this error. Your links was very usefull.
     
    Kurt-Dekker likes this.