Problem with event handling in Unity

Post here your questions about the Unity / .Net / Mono / Windows 8 / Windows Phone 8 API for SFS2X

Moderators: Lapo, Bax

caseyryan
Posts: 6
Joined: 19 Sep 2018, 12:45

Problem with event handling in Unity

Postby caseyryan » 19 Sep 2018, 12:51

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?
User avatar
Bax
Site Admin
Posts: 4409
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: Problem with event handling in Unity

Postby Bax » 19 Sep 2018, 14:31

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();
   }
Paolo Bax
The SmartFoxServer Team
caseyryan
Posts: 6
Joined: 19 Sep 2018, 12:45

Re: Problem with event handling in Unity

Postby caseyryan » 19 Sep 2018, 17:33

Thanks! It works :)
By the way, this code is present in the examples, but I've missed out on it. My fault :o

Return to “SFS2X C# API”

Who is online

Users browsing this forum: No registered users and 7 guests