"Can only be called from the main thread" error in editor.

Hi Guys,

I’m getting an error in the editor that doesnt occur in the compiled version.

I have a static class “StaticData” that does not derive from MonoBehaviour. htRemotePlayers is a static hashtable that I keep a list of GameObjects (remote players) in.

When a remote player moves smartfox envokes an event and I get the corresponding object from the hashtable:

GameObject gObj = (GameObject)(StaticData.htRemotePlayers[sUserName]);

Whenever I try to do anything to gObj I get the error:

…Can only be called from the main thread.

So if I try to call

RemotePlayerScript CurrentRemotePlayerScript = (RemotePlayerScript)gObj.GetComponent(“RemotePlayerScript”);

I catch it in my try block and get the error “GetComponentByName Can only be called from the main thread”.

However when I compile the project I dont have the same problem. I am using Unity 3 pro… any ideas??

I should also add that I have moved all variable initialisations to “Start” where possible and initialised the globals in the StaticData class in a “Initialise” class thats called very early on.

did you ensure to use smartfox the way that it uses the unity mainthread for the event callback handling, not running async in an own thread (as you can use it on .net otherwise) and firing in from there. because if it runs in an own thread you get exactly the errors you get there as unity does not allow another thread calling into any unity engine related thing at all from outside. you must “call out” from within the engine

How do I ensure this dreamora?

did you use the example that comes for unity? if so it does that. But if you used the .net example as base you are likely going the “wrong way”

Well I started with the Smartfox unity example. Do I have to define delegates and somehow tack onto the main thread?

Looking into something called smart fox “queue mode” looks like it can clear up some thread issues related to sfs vs Unity

That mode should normally be active by default in the unity example.

I checked (the lobby chat example) its not active, not sure how this impacts everything else,

Ok guys I followed the island demo and im not getting the error anymore. Thanks dreamora.