Search Issue Tracker

By Design

Votes

1

Found in [Package]

1.7.8

Issue ID

UVSB-2379

Regression

No

ArgumentNullException when trying to check if EventHook == null

Package: Visual Scripting

-

How to reproduce:
1. Open the user's attached project
2. Enter Play mode
3. Observe the Console

Expected result: There are no errors in the Console
Actual result: Console produces the "ArgumentNullException: Value cannot be null." error

Reproducible with: 1.7.8 (2021.3.4f1, 2022.1.5f1, 2022.2.0a16)
Couldn't test with: 2019.4.39f1, 2020.3.36f1 (No Visual Scripting version available)

Reproducible on: Windows 10

Note: Script that causes the error can be found in Assets/Scripts

  1. Resolution Note:

    The issue is in the user's project (see details in comments)

Comments (2)

  1. jeanedouard_unity

    Aug 24, 2022 17:46

    The user uses the following code that breaks :

    EventHook m_eventHook;
    void Start()
    {
    m_eventHook = new EventHook("DoWork");
    EventBus.Register<int>(m_eventHook, DoWork);
    }

    private void Update()
    {
    if (m_eventHook != null)
    {
    EventBus.Trigger(m_eventHook, 1);
    }
    }
    But EvenHook is a struct and structs are value types. This means it cannot be null and when checking if the eventHook is null it falls back on a default implicit operator which is by design. If users want to check if the EventHook is null they must use the Nullable Value Types C#8 feature. Here is how the code should look like:

    EventHook? m_eventHook;
    void Start()
    {
    m_eventHook = new EventHook("DoWork");
    EventBus.Register<int>(m_eventHook.Value, DoWork);
    }

    private void Update()
    {
    if (m_eventHook != null)
    {
    EventBus.Trigger(m_eventHook.Value, 1);
    }
    }

  2. SmartMediaNL

    Jun 10, 2022 10:57

    What happens is that the check to see if the EventHook is null will in turn (internally by Unity Engine) do its own even hook call with a string that is null resulting in the error thrown mentioned above.

Add comment

Log in to post comment

All about bugs

View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.