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
-
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
- The composition string is not cleared and is transferred to another Input Field when the previous Input Field loses focus in the macOS Player
- ShadowCaster2D does not update when using Sprite flipbook animation
- PropertyField with range attribute triggers change event when the value didn't change
- [Android] Soft keyboard is missing the "OK" button on Android
- The Profiler ignores a modified targetFrameRate when profiling Edit mode
Resolution Note:
The error is in the user's code. (See comments for details)