Search Unity

NullReferenceException while executing 'started' callbacks of...

Discussion in 'Input System' started by sirmarcomoro, Oct 25, 2020.

  1. sirmarcomoro

    sirmarcomoro

    Joined:
    Mar 22, 2018
    Posts:
    4
    Hi, I am getting this error after I press the relative key:
    NullReferenceException while executing 'started' callbacks of 'DefaultControls/SelectOption2[/Keyboard/2]'
    UnityEngine.InputSystem.LowLevel.<>c__DisplayClass7_0:<set_onUpdate>b__0(NativeInputUpdateType, NativeInputEventBuffer*)
    UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate(NativeInputUpdateType, IntPtr) (at /Users/builduser/buildslave/unity/build/Modules/Input/Private/Input.cs:120)

    Code is the following. I am just using Invoke Unity Events, with the buttons set as Pass Through > Button > Press Only. If I don't put ctx.started in the if statement I get a NullRef even for "performed" and "canceled".

    I am just trying to make a simple button for my prototype, for heaven's sake. I HAVE to use the new input system for unrelated reasons. Thanks.


    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. using UnityEngine.InputSystem;
    4. using static UnityEngine.InputSystem.InputAction;
    5. using System;
    6. using System.Linq;
    7. using DialogueEditor;
    8.  
    9. public class InputHandler : MonoBehaviour
    10. {
    11.     private PlayerInput input;
    12.     private ThirdPersonMovement playerMovement;
    13.     private ConversationHandler conversation;
    14.     private int playerIndex;
    15.  
    16.     private void Awake()
    17.     {
    18.         input = GetComponent<PlayerInput>();
    19.         conversation = ConversationHandler.Instance;
    20.  
    21.         //Assign the instance of PlayerInput to the correct player
    22.         var index = input.playerIndex;
    23.         var playerMovementScripts = FindObjectsOfType<ThirdPersonMovement>();
    24.         playerMovement = playerMovementScripts.FirstOrDefault(m => m.GetComponentInParent<Index>().GetIndex() == index);
    25.  
    26.         playerIndex = playerMovement.GetComponentInParent<Index>().GetIndex();
    27.     }
    28.  
    29.     //Move the player
    30.     public void OnMove(CallbackContext ctx) { if(playerMovement != null) { playerMovement.InputVector = new Vector3(ctx.ReadValue<Vector2>().x, 0, ctx.ReadValue<Vector2>().y); } }
    31.  
    32.     //Input selected option
    33.     public void OnSelectOption1(CallbackContext ctx) { if(ctx.started && conversation.selectionSentences != null && conversation.selectionSentences.Count > 0) { SelectSentence(0); } }
    34.     public void OnSelectOption2(CallbackContext ctx) { if(ctx.started && conversation.selectionSentences != null && conversation.selectionSentences.Count > 1) { SelectSentence(1); } }
    35.     public void OnSelectOption3(CallbackContext ctx) { if(ctx.started && conversation.selectionSentences != null && conversation.selectionSentences.Count > 2) { SelectSentence(2); } }
    36.  
    37.     //Send selected sentence and index
    38.     private void SelectSentence(int n) { conversation.selectedSentences.Add(new Tuple<SpeechNode, int>(conversation.selectionSentences[n].Dialogue, playerIndex)); }
    39. }
    40.  

     
  2. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    The error messages *after* the log for the NRE is the actual exception dump. The initial message only gives context for the exception but the NRE happens inside the callback.

    The "two messages logged for one error" behavior probably isn't the best and probably could use some tweaking.
     
    mopthrow likes this.
  3. sirmarcomoro

    sirmarcomoro

    Joined:
    Mar 22, 2018
    Posts:
    4
    Any idea on how I could go around solving it? It's a prototype, I just need a button press functionality... If I need to change some code I'd be down for that. Thanks
     
  4. sirmarcomoro

    sirmarcomoro

    Joined:
    Mar 22, 2018
    Posts:
    4
    Wait, do you mean to say that the actual error is not that, but a second error I might be getting? Because I am getting another error, that doesn't have anything to do with the input system, or at least I think:

    NullReferenceException: Object reference not set to an instance of an object
    InputHandler.OnSelectOption2 (UnityEngine.InputSystem.InputAction+CallbackContext ctx) (at Assets/Scripts/Player Characters/InputHandler.cs:33)
    UnityEngine.Events.InvokableCall`1[T1].Invoke (T1 args0) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent.cs:221)
    UnityEngine.Events.UnityEvent`1[T0].Invoke (T0 arg0) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent/UnityEvent_1.cs:58)
    UnityEngine.InputSystem.Utilities.DelegateHelpers.InvokeCallbacksSafe[TValue] (UnityEngine.InputSystem.Utilities.InlinedArray`1[System.Action`1[TValue]]& callbacks, TValue argument, System.String callbackName, System.Object context) (at Library/PackageCache/com.unity.inputsystem@1.0.0/InputSystem/Utilities/DelegateHelpers.cs:51)
    UnityEngine.InputSystem.LowLevel.<>c__DisplayClass7_0:<set_onUpdate>b__0(NativeInputUpdateType, NativeInputEventBuffer*)
    UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate(NativeInputUpdateType, IntPtr) (at /Users/builduser/buildslave/unity/build/Modules/Input/Private/Input.cs:120)
     
  5. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Yup, that's what I meant. The first error is the input system saying "hey, an exception occurred in a callback" and the second error is the actual exception. The actual NRE comes from InputHandler.cs@33 in your case.
     
  6. deerymaslo

    deerymaslo

    Joined:
    Jun 12, 2021
    Posts:
    1
    So, how do i solve it?
     
  7. Rambo_Dash

    Rambo_Dash

    Joined:
    Aug 14, 2021
    Posts:
    1
    Sorry, if I'm late for to party.
    It depends on the error you're getting. If you still need help, tell what error you're getting, so I can try to help.
     
  8. spunkyschild

    spunkyschild

    Joined:
    Oct 3, 2022
    Posts:
    2
    o_O
    im getting an error that says "NullReferenceException while executing 'performed' callbacks of 'Player/Jump[/DualShock4GamepadHID/buttonSouth]'" how can i fix this
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.     private float moveInput;
    9.     public float speed;
    10.     public float jumpForce;
    11.  
    12.     public float checkRadius;
    13.     public LayerMask groundCheck;
    14.     public Transform playerPos;
    15.  
    16.     private Rigidbody2D rb;
    17.  
    18.     private bool facingRight = true;
    19.  
    20.     void Start()
    21.     {
    22.         rb = GetComponent<Rigidbody2D>();
    23.     }
    24.     void Update()
    25.     {
    26.         if (!facingRight && moveInput > 0)
    27.         {
    28.             Flip();
    29.         }
    30.         if (facingRight && moveInput < 0)
    31.         {
    32.             Flip();
    33.         }
    34.     }
    35.  
    36.     void FixedUpdate()
    37.     {
    38.         rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
    39.     }
    40.  
    41.     void Flip()
    42.     {
    43.         facingRight = !facingRight;
    44.         Vector3 Scaler = transform.localScale;
    45.         Scaler.x *= -1;
    46.         transform.localScale = Scaler;
    47.     }
    48.  
    49.     public void Move(InputAction.CallbackContext context)
    50.     {
    51.         moveInput = context.ReadValue<Vector2>().x;
    52.     }
    53.  
    54.     public void Jump(InputAction.CallbackContext context)
    55.     {
    56.         if (context.performed && IsGrounded())
    57.         {
    58.             rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    59.         }
    60.  
    61.         if (context.canceled && rb.velocity.y > 0f)
    62.         {
    63.             rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.7f);
    64.         }
    65.     }
    66.  
    67.     private bool IsGrounded()
    68.     {
    69.         return Physics2D.OverlapCircle(playerPos.position, checkRadius, groundCheck);
    70.     }
    71. }
     
    Last edited: Nov 5, 2022
  9. aloften

    aloften

    Joined:
    Aug 25, 2017
    Posts:
    18
    Hey dude. I just got this same error and it was pretty confusing. I think its meant to mean more detail but its misleading.

    My bug had nothing to do with the button that I pressed, but instead was telling me that it happened as a result of the function called(like an error in the function the button started).

    If your bug is similar to mine, the first place to look would be " public void Jump"

    I would make sure everything is returning a value. Try removing code from that function and adding it back one piece at a time. Also note that this error might not be in the jump function but could be in a function called from the jump function. Maybe your "IsGrounded" function has an error. Or maybe your variable "rb" is returning null.

    BUT IF I WAS TO STAB IN THE DARK....looks like youre trying to return a bool when youre actually returning colliders in IsGrounded. If im right, then you are getting an error in jump because it was called after your button was pressed as a chain reaction. Hope it helps.
     
    AlvaroVelasco likes this.
  10. SuperPox

    SuperPox

    Joined:
    Apr 29, 2020
    Posts:
    12
    I got the error because I was subbed to the same input action on 2 running scripts.
     
  11. SamClothier

    SamClothier

    Joined:
    Feb 16, 2023
    Posts:
    1
    For me, Unity just needed restarting.