Search Unity

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

Discussion in 'Scripting' started by selzero, Oct 13, 2010.

  1. selzero

    selzero

    Joined:
    Dec 3, 2009
    Posts:
    32
    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??
     
  2. selzero

    selzero

    Joined:
    Dec 3, 2009
    Posts:
    32
    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.
     
  3. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    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
     
  4. selzero

    selzero

    Joined:
    Dec 3, 2009
    Posts:
    32
    How do I ensure this dreamora?
     
  5. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    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"
     
  6. selzero

    selzero

    Joined:
    Dec 3, 2009
    Posts:
    32
    Well I started with the Smartfox unity example. Do I have to define delegates and somehow tack onto the main thread?
     
  7. selzero

    selzero

    Joined:
    Dec 3, 2009
    Posts:
    32
    Looking into something called smart fox "queue mode" looks like it can clear up some thread issues related to sfs vs Unity
     
  8. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    That mode should normally be active by default in the unity example.
     
  9. selzero

    selzero

    Joined:
    Dec 3, 2009
    Posts:
    32
    I checked (the lobby chat example) its not active, not sure how this impacts everything else,
     
  10. selzero

    selzero

    Joined:
    Dec 3, 2009
    Posts:
    32
    Ok guys I followed the island demo and im not getting the error anymore. Thanks dreamora.