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
- Mono Windows Builds don't produce full log callstacks when generating logs
- AssetBundles fail to load when running in Built Players for Mobile Devices
- UI elements with text gets bigger and grey when Player window is moved to another screen with different resolution
- System name accepts multiline text but crops it on confirmation, duplicates input, and shrinks the field when empty
- UI element scale and position are wrong in project build when DRS is changed with HDR and Software Dynamic Resolution enabled
Resolution Note:
The error is in the user's code. (See comments for details)