Page 1 of 1

Problem with event handling in Unity

Posted: 19 Sep 2018, 12:51
by caseyryan
I've got a problem with unity client API.
If I set ThreadSafeMode to false, smartfox fires events normally but when I try to access any game objects from the event handler I get this error in unity
get_gameObject can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
UnityEngine.Component:get_gameObject(Component)

But if I set ThreadSafeMode to true, SmartFox never fires any events.
It seems like a complete stalemate.
Is there any way to get around this?

Re: Problem with event handling in Unity

Posted: 19 Sep 2018, 14:31
by Bax
ThreadSafeMode must always be "true".
Also, make sure you always call the sfs.ProcessEvents() method in the MonoBehaviour.Update method, like this:

Code: Select all

   void Update() {
      if (sfs != null)
         sfs.ProcessEvents();
   }

Re: Problem with event handling in Unity

Posted: 19 Sep 2018, 17:33
by caseyryan
Thanks! It works :)
By the way, this code is present in the examples, but I've missed out on it. My fault :o