So I am using parse.com(a backhand database provider, made by facebook) for my unity project. I got stuck with a serious problem. Here is the explanation;
as long as i throw out my Synchronization codes from my game everything works fine but i need it in order to make the player sync their data to the server.
So here is the error im an getting:
get_version 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.
And here is my user.cs code:
using UnityEngine;
using System.Collections;
using System.Threading;
using Parse;
public class User : MonoBehaviour {
public bool isAuthenticated = false;
public string userName = null;
public int level;
public int exp;
public int money;
public GoLobby golobby;
public Sync sync;
public bool isGoLobbyEnabled = false;
public void LoginUser(string username, string password)
{
var query = ParseObject.GetQuery("Member")
.WhereEqualTo("Username", username)
.WhereEqualTo("Password", password);
query.FirstOrDefaultAsync().ContinueWith(t =>
{
ParseObject result = t.Result;
//print (result);
string playerName = result.Get<string>("Username");
string playerPassword = result.Get<string>("Password");
int playerLevel = result.Get<int>("Level");
int playerexp = result.Get<int>("Experience");
int playerMoney = result.Get<int>("Money");
isAuthenticated = true;
userName = playerName;
money = playerMoney;
exp = playerexp;
sync.SyncToServer (userName, level, exp, money);
isGoLobbyEnabled = true;
GoLobby();
});
}
Here is my Sync.cs code:
using UnityEngine;
using System.Collections;
using Parse;
public class Sync : MonoBehaviour {
public void SyncToServer (string username, int level, int exp, int money)
{
var query2 = ParseObject.GetQuery ("Member")
.WhereEqualTo ("Username", username);
query2.FirstAsync ().ContinueWith (t =>
{
ParseObject obj = t.Result;
Debug.Log (obj.ObjectId); //Works fine
obj["Level"] = level;
obj["Experience"] = exp;
obj["Money"] = money;
obj.SaveAsync();
});
}
}