Sign In Register

How can we help you today?

Start a new topic
Answered

Getting "ToString can only be called from the main thread" error in Unity

Hi there, I'm getting an error when testing my password recovery function in the Unity 5.6.3p1 editor (Android) with GameSparks' Unity SDK v5.5.7.139 on a windows 10 machine

The error log is

ToString 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.

  

The full stack trace of the error log is

[Error] ToString 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.
0. UnityEngine.Object.ToString()
1. GameSparks.Core.Serializer.SerializeOther()    c:/jenkins/workspace/clientsdk-dotnet-rt-tls/GameSparks.Api/Core/GSJson.cs:486
2. GameSparks.Core.Serializer.SerializeValue()    c:/jenkins/workspace/clientsdk-dotnet-rt-tls/GameSparks.Api/Core/GSJson.cs:385
3. GameSparks.Core.Serializer.SerializeObject()    c:/jenkins/workspace/clientsdk-dotnet-rt-tls/GameSparks.Api/Core/GSJson.cs:402
4. GameSparks.Core.Serializer.SerializeValue()    c:/jenkins/workspace/clientsdk-dotnet-rt-tls/GameSparks.Api/Core/GSJson.cs:379
5. GameSparks.Core.Serializer.SerializeObject()    c:/jenkins/workspace/clientsdk-dotnet-rt-tls/GameSparks.Api/Core/GSJson.cs:402
6. GameSparks.Core.Serializer.SerializeValue()    c:/jenkins/workspace/clientsdk-dotnet-rt-tls/GameSparks.Api/Core/GSJson.cs:379
7. GameSparks.Core.Serializer.Serialize()    c:/jenkins/workspace/clientsdk-dotnet-rt-tls/GameSparks.Api/Core/GSJson.cs:353
8. GameSparks.Core.GSJson.To()    c:/jenkins/workspace/clientsdk-dotnet-rt-tls/GameSparks.Api/Core/GSJson.cs:340
9. GameSparks.Core.GSData.get_JSON()    c:/jenkins/workspace/clientsdk-dotnet-rt-tls/GameSparks.Api/Core/GSData.cs:27
10. GameSparks.Core.GSConnection.SendImmediate()    c:/jenkins/workspace/clientsdk-dotnet-rt-tls/GameSparks.Api/Core/GSConnection.cs:210
11. GameSparks.Core.GSInstance.ProcessSendQueue()    c:/jenkins/workspace/clientsdk-dotnet-rt-tls/GameSparks.Api/Core/GSInstance.cs:818
12. GameSparks.Core.GSInstance.ProcessQueues()    c:/jenkins/workspace/clientsdk-dotnet-rt-tls/GameSparks.Api/Core/GSInstance.cs:558
13. GameSparks.Core.GameSparksTimer.TimerCallback()    c:/jenkins/workspace/clientsdk-dotnet-rt-tls/GameSparks/GameSparksTimer.cs:33
14. System.Timers.Timer.Callback()

 

The code that triggered this is

    public void ResetPassword (string token, string password) {

        //Sending the request with spaces for Username and Password so no errors are given and scriptData is sent
        //Response is breaken down and the result of the action is determined for debug or feedback to user
        GSRequestData script = new GSRequestData();

        script.Add("action", "resetPassword");
        script.Add("token", token);
        script.Add("password", password);

        //Sending the request with spaces for Username and Password so no errors are given and scriptData is sent
        //Response is breaken down and the result of the action is determined for debug or feedback to user
        AuthenticationRequest sAuthenticationRequest = new AuthenticationRequest();
        sAuthenticationRequest.SetUserName("");
        sAuthenticationRequest.SetPassword("");
        sAuthenticationRequest.SetScriptData(script);

        sAuthenticationRequest.Send((response) => {

        });

    }

This is a modified but essentially the same as one of your tutorials for password recovery.

Is anyone else able to repro this issue or seems something similar?


Best Answer

Hey There,

This is a unity error associated with an object not being created before calling .ToString() on it.
Without knowing where you are calling ResetPassword() its hard to tell what the problem is, but it sounds like you are calling it from an object before it is full constructed.


You would usually get this when attempting to call a function from a constructor. If you are doing this, try moving it to Awake instead.

Thanks,
Sean

1 Comment

Answer

Hey There,

This is a unity error associated with an object not being created before calling .ToString() on it.
Without knowing where you are calling ResetPassword() its hard to tell what the problem is, but it sounds like you are calling it from an object before it is full constructed.


You would usually get this when attempting to call a function from a constructor. If you are doing this, try moving it to Awake instead.

Thanks,
Sean

Login to post a comment