Search Unity

Question NullRef Exceptions and other mysterious behaviours

Discussion in 'Input System' started by sirmarcomoro, Nov 4, 2020.

  1. sirmarcomoro

    sirmarcomoro

    Joined:
    Mar 22, 2018
    Posts:
    4
    Hi,

    In this previous post I encountered a weird behaviour in my InputHandler script and it developed into something truly mysterious, so I am bringing it back here in the hopes someone is able to help me shed some light into what is exactly is happening. I have a list of observations, that may very well be unrelated to each other but are nonetheless giving me hell.

    First, the code:

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

    Code (CSharp):
    1. public void OnSelectOption1(CallbackContext ctx) { if(ctx.started && conversation.selectionSentences != null && conversation.selectionSentences.Count > 0) { SelectSentence(0); } }
    2.     public void OnSelectOption2(CallbackContext ctx) { if(ctx.started && conversation.selectionSentences != null && conversation.selectionSentences.Count > 1) { SelectSentence(1); } }
    3.     public void OnSelectOption3(CallbackContext ctx) { if(ctx.started && conversation.selectionSentences != null && conversation.selectionSentences.Count > 2) { SelectSentence(2); } }
    Basically, trying to select a dialogue option on a button press.

    You can read the exceptions on the original post but I am now convinced that they are of little importance really. It just tells me that something (that I could not narrow down in any way, because of all of the observations below, but presumably in the if statement) is not defined as a reference of an object. Still, I may be wrong.

    Now, the observations:

    • If statement and its content runs perfectly well on Awake
    • Content of the if statement doesn’t seem to run at all after exception (this may be normal but idk)
    • Debug.Log(conversation), (conversation.selectionSentences) and (conversation.selectionSentences.Count) all return the expected results on Awake (and without the exception) but return a weird extra “Null” in custom method (and with the exception):
    Pasted Graphic.png
    • Error doesn’t happen if there’s no content in method, but does if there is ANY content, including even just a Debug.Log
    All of this is with PressOnly as an Interaction in the Actions. If I remove that:
    • Removing PressOnly as the Action Interaction makes the error go away
    • The code still doesn’t execute
    • A simple Debug.Log(“something”) inside the methods runs without any errors, but Debug.Log(conversation.selectionSentences != null), while returning true, makes the error re-appear
    After that, I tried compacting OnSelectOption1/2/3 to a single method OnSelectDialogueOption, with the intent of simply checking which of the bindings was pressed to determine if it should be option 1, 2 or 3, something that by the way I realised I don't know how to do, and started testing.

    All of the above seems to apply as before, unsurprisingly.
    Then, I tried putting a
    if (!ctx.started) { return; }
    before the rest of the method, and it would never go past it.
    So I changed it to
    if (!ctx.started) { Debug.Log("No way"); return; }
    and saw the last mystery: it fires 4 times when pressing and releasing, and an additional 2 times when I stop the game running! I am honestly quite a bit lost, and so is my lecturer by the way, who didn't find a way to help me either.

    I tried implementing the Input System some other way, subscribing to the actions, but it turned out to be another pain in the *** and I am honestly starting to be a bit sad about this whole ordeal.

    Please almighty Unity gods, help me.
     
    Last edited: Nov 4, 2020