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
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
Comments (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
All about bugs
View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.
Latest issues
- "One or more data file missing for baking set NewScene Baking Set. Cannot load shared data." error in Player when a specific project is built
- Choosing new HDR Colour using RGB values breaks colour on Intensity Selectors
- Rendering/Decal Layer Mask options are different inside Prefab Mode and outside Prefab Mode when the project is upgraded to Unity 6
- Incorrect Realtime GI Light Probes baking when more than one Light Probe Group is used and "Baked Global Illumination" is enabled
- Hovering on the three-dot menu also highlights the tab header near it when not all tabs fit the bar
Resolution Note:
The error is in the user's code. (See comments for details)