0

I use Unity3d for android platform,

I'm getting an error like below when I try to show a game object in onRoomJoin event.

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()
SFSConnect:OnRoomJoin(BaseEvent) (at Assets/SFSConnect.cs:861)
Sfs2X.Core.EventDispatcher:DispatchEvent(BaseEvent)
Sfs2X.SmartFox:DispatchEvent(BaseEvent)
Sfs2X.Controllers.SystemController:FnJoinRoom(IMessage)
Sfs2X.Controllers.SystemController:HandleMessage(IMessage)
Sfs2X.Core.SFSProtocolCodec:DispatchRequest(ISFSObject)
Sfs2X.Core.SFSProtocolCodec:OnPacketRead(ByteArray)
Sfs2X.Core.SFSIOHandler:HandlePacketData(ByteArray)
Sfs2X.Core.SFSIOHandler:OnDataRead(ByteArray)
Sfs2X.Bitswarm.BitSwarmClient:OnSocketData(Byte[])
Sfs2X.Core.ThreadManager:ProcessItem(Hashtable)
Sfs2X.Core.ThreadManager:InThread()

Here is my code as shown below:

public class SFSConnect : MonoBehaviour {

    public GameObject obj;

        void Start () {

        img = obj.GetComponent<Image> ();

        img.obj.SetActive (false);


        }

    void onRoomJoin(BaseEvent evt)
    {

        img.obj.SetActive (true);

    }

}
Muhammet Demir
  • 1,995
  • 5
  • 21
  • 42
  • Where are you calling onRoomJoin from? Is that being called from a plugin? – Programmer Aug 23 '17 at 17:55
  • I use smartfoxapi, when I join smartfoxserver room it calls onRoomJoin event and I join room when scene is built – Muhammet Demir Aug 23 '17 at 17:57
  • It is likely designed to make the callback in another Thread. See the dup for a script I made that you can use to run `img.obj.SetActive (true);` in the main thread. – Programmer Aug 23 '17 at 17:59
  • @Programmer When I call `SetActive` from main thread like `Start()` , no errors are thrown. It happens only when I call it In `onRoomJoin(BaseEvent evt)` function, which makes me think that it's a different situation you've pointed as duplicate thread. – Muhammet Demir Aug 23 '17 at 18:17
  • *"When I call SetActive from main thread like Start() , no errors are thrown"* That's because `Start` is being called in the main Thread. Almost all Unity callback functions are being called in the main thread except for the audio callback function and few others. *"It happens only when I call it In onRoomJoin(BaseEvent evt) function"*...Again, that's because `onRoomJoin` is being called from another Thread that's not a main Thread. You can't use most Unity's API in another Thread. Using the function in my dup to make that call should in the `onRoomJoin` func should fix it. – Programmer Aug 23 '17 at 18:21
  • @Programmer I use UnityThread script but `void Update` function doesn't triggered, what should I do? – Muhammet Demir Aug 30 '17 at 16:18
  • You are suppose to attach `UnityThread` script to an empty GameObject. That's it. How to use it on the other answer. Please don't comment multiple places if you want a reply. Just comment on one place. – Programmer Aug 30 '17 at 17:45
  • So sorry, I don't understand what you said – Muhammet Demir Aug 30 '17 at 18:22
  • 1.Attach the `UnityThread` script to an empty GameObject. Do you know how to do this? – Programmer Aug 30 '17 at 18:32
  • I fixed this, thanks, so can you help me another issue? how to useUnityThread.executeInUpdate method with parameter like this `void example(string a, int b)` – Muhammet Demir Aug 30 '17 at 20:17
  • How about you read the answer from the duplicate. It explains how to use that under where it says **"USAGE"**. `UnityThread.executeInUpdate(() => { example("Hellow", 4); });`.... Also, you don't have to attach the `UnityThread` script to an empty GameObject like I mentioned above. Just call `UnityThread.initUnityThread();` from the your scripts's `Awake` function and it will attach it for you. Just read that answer carefully since it explains these pretty much. – Programmer Aug 30 '17 at 20:25
  • Thanks in advance, I already use that you mentioned, just calling awake function this `UnityThread.initUnityThread();` – Muhammet Demir Aug 30 '17 at 21:16

0 Answers0