- Home /
Why does my EventHandler listener method not finish execution?
I have integrated Firebase Database into my Unity project. I have a FirebaseInit script and a FriendManager script. I've set up a UnityEvent to be invoked once the Firebase "CheckAndFixDependenciesAsync" is completed and I've set up my Firebase database reference. This event that is invoked triggers a listener in my FriendManager that then initializes a friendslist using the Firebase Database reference.
Here's the problem: In the method that is triggered in my FriendManager class when the event is invoked ("HandleFriendManagerInit"), I see a Debug.Log call that tells me the code is being executed. But then I have further calls as the code executes that never get called. And, the code that should execute (ie: get the friendslist from Firebase and use it to populate a DropDown UI element) doesn't execute.
Question: is there something with threading model in Unity that could cause the eventhandler method being triggered by the listener to start execution but never finish?
Answer by JB2019 · Aug 25, 2020 at 03:15 AM
Update to the above - I did some deeper debugging and discovered the thread was hitting some kind of exception that killed the thread. It was caused by references to GameObject.Find and .GetComponent<...>()... I ended up removing those calls from my EventHandling function and now the method completes properly.
NEW Question is - why would references to GameObject.Find or .GetComponent cause an exception to be hit that would kill that thread?
Thank you! Solved my problem immediately. Just for future debugging, can I ask how you figured that out? Like what tools you used?
To answer @JB2019 's question, Unit is **not** thread safe, meaning that using Unity's API is only allowed from the main thread. There are things that you can do to get multithreaded programs working, but they all involve offloading all API calls to the main thread.
@stevencr4z - I use Visual Studio Code alongside Unity and set up debugging (set VS Code as. your external script editor in Unity>Preferences, and download the Debugger for Unity extension in VS Code). Pretty helpful!
@unity_ek98vnTRplGj8Q - thanks for the response and yes, I've since had to move certain calls back to main thread execution to work.
Your answer
Follow this Question
Related Questions
Coroutine Death on Scene Change 3 Answers
How to handle a slow function from a dll 1 Answer
Return value from coroutine to non monobehaviour 1 Answer
Instantiating many objects during update lags 1 Answer
Coroutine - events not working 2 Answers