Hi,
I’m trying to access GameObjects inside Nakama callbacks, but it seems not to work?
Here is what I did: https://paste.ofcode.org/qqXump6gWWBjUHKLvCzH2b
Maybe i miss something?
Thanks for your help
Hi,
I’m trying to access GameObjects inside Nakama callbacks, but it seems not to work?
Here is what I did: https://paste.ofcode.org/qqXump6gWWBjUHKLvCzH2b
Maybe i miss something?
Thanks for your help
Hi - could you be more descriptive about “it seems not to work?”
Are you getting an error? What is it and what’s the stacktrace?
Hello,
This is a common pitfall with Unity and Threading, were non-main threads fail silently when trying to use Unity API. Use a try-catch block and Debug.Log the exception in order to get the error. Debug functions are thread-safe allowing easy debugging of this.
What I think is happening is passing an async function to the socket event is making it create a non-unity thread, which causes the error described above.
Hi
Thx superxwolf.
Indeed I now have a error log:
GetName 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.Debug:Log(Object)
<b__1>d:MoveNext() (at Assets/MainScript.cs:42)
System.Runtime.CompilerServices.AsyncVoidMethodBuilder:Start(<b__1>d&)
<>c__DisplayClass1_0:b__1(IMatchmakerMatched)
Nakama.Socket:ReceivedMessage(ArraySegment1) Nakama.<ReceiveLoop>d__33:MoveNext() System.Runtime.CompilerServices.AsyncTaskMethodBuilder
1:SetResult(WebSocketReceiveResult)
Nakama.Ninja.WebSockets.Internal.d__33:MoveNext()
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1:SetResult(WebSocketFrame)
Nakama.Ninja.WebSockets.Internal.d__0:MoveNext()
System.Runtime.CompilerServices.AsyncTaskMethodBuilder:SetResult()
Nakama.Ninja.WebSockets.Internal.d__0:MoveNext()
System.Threading._ThreadPoolWaitCallback:PerformWaitCallback()
Any idea what to do? I’m pretty new to unity
Hello,
For now you can just pass a non-async function to the events, like you did with connected, and then call an async void function from there. That should give you an async call in the unity thread.
@Mkarena https://github.com/PimDeWitte/UnityMainThreadDispatcher this might help.
@hdjay0129 Thanks, it works with this.
Is this a Nakama problem or me using Nakama wrong or using this third party library is just fine?
@Mkarena There’s no problem with Nakama. It’s actually a problem in Unity itself.
All Unity calls run inside the main thread, you can’t access Unity code paths and components inside other threads. To do so you can create your own dispatcher to move calls onto the main thread, or just work with existing third party packages.
https://github.com/PimDeWitte/UnityMainThreadDispatcher this might help.
I know this reply is too late, I also faced same issue and some how landed on this thread.
This issue wasted my 4-5 hours, but I found much simpler and quick fix. So, when you are creating socket just use following
var socket = client.NewSocket(useMainThread: true);