floatinghotpot / socket.io-unity

socket.io client for Unity, power game client with node.js back-end
494 stars 76 forks source link

xxxxxxx can only be called from the main thread. #25

Open ronaldovelo opened 6 years ago

ronaldovelo commented 6 years ago

I started today using your socket-io asset, and I have been trying to understand the problem, I did understand in fact, but I dont know how to work-arround.

This is the event

socket.On("CLIENT_SUCCESSFUL_LOGIN", (data) =>
            {
                string dataS = data.ToString();
                Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(dataS);
                string _userID = values["userId"];
                float _funds = float.Parse(values["funds"]);
                SuccessfulLogin(_userID, _funds);
            });

And I want execute a function and it gives me that error...

void SuccessfulLogin(string _userId, float _funds)
    {
        unlockedItems.Clear();
        userId = _userId;
        funds = _funds;
        isLogged = true;
        menuManager.ChooseMenu(1);
    }
public void ChooseMenu(int menuId)
    {
        if (menuId < menuPanels.Count && menuId >= 0)
        {
            DisableCanvasGroup(menuPanels[activePanel].GetComponent<CanvasGroup>());
            EnableCanvasGroup(menuPanels[menuId].GetComponent<CanvasGroup>());
            activePanel = menuId;
            UpdateFields();
        }
    }
Zbluu commented 6 years ago

This is not a issue relative to this asset.

https://stackoverflow.com/questions/41330771/use-unity-api-from-another-thread-or-call-a-function-in-the-main-thread

I use the solution of the first answer, it's works.