Search Issue Tracker

By Design

Votes

0

Found in [Package]

1.7.8

Issue ID

UVSB-2385

Regression

No

Using EventBus causes a MissingReferenceException to appear when re-entering Play mode

Package: Visual Scripting

-

How to reproduce:
1. Open the user's attached "MyTest" project
2. Open the "TestScene" scene
3. Enter Play mode
4. Exit Play mode
5. Enter Play mode
6. Press Spacebar
7. Observe the Console

Expected result: "DemoScriptDoWork" logs appear
Actual result: This error appears: "MissingReferenceException: The object of type 'DemoScript' has been destroyed but you are still trying to access it."

Reproducible with: 1.7.8 (2021.3.6f1, 2022.1.8f1, 2022.2.0a19, 2023.1.0a3)
Couldn't test with: 2020.3.37f1 (Console errors)

Reproducible on: Windows 10

  1. Resolution Note:

    The error is in the user's code. (See comments for details)

Comments (1)

  1. jeanedouard_unity

    Aug 24, 2022 17:49

    When using the EventBus by code it is the responsibility of the user to Register and Unregister the events. Because in the user's code the event has not been unregistered, it is normal that it creates errors between PlayModes as the EventBus is static so it keeps every reference that have been registered. Here is how the code should look like to avoid errors:

    public class DemoScript : MonoBehaviour
    {
    Delegate m_DoWork;

    void Start()
    {
    Action<int> dw = i => DoWork(i);
    m_DoWork = dw;
    EventBus.Register<int>("DoWork", dw);
    }

    private void DoWork(int obj)
    {
    Debug.Log(this.name + "DoWork");
    }

    void OnDestroy()
    {
    EventBus.Unregister("DoWork", m_DoWork);
    Debug.Log("OnDestroy");
    }
    }

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.