How to update score in firebase instead of replacing it?
Whenever I reopen the game and start scoring again it replaces the previous data. Please help me with this and tell me where I'm doin it wrong.
I'm using Firebase Realtime Database.
This is my score script down below.
public static int cashValue;
Text cash;
void Start()
{
cash = GetComponent();
}
void Update()
{
cash.text = "" + cashValue;
}
This is where I save the score in Firebase Realtime Database.
private IEnumerator UpdateKills(int _kills)
{
//Set the currently logged in user kills
var DBTask = DBreference.Child("users").Child(User.UserId).Child("kills").SetValueAsync(_kills);
yield return new WaitUntil(predicate: () => DBTask.IsCompleted);
if (DBTask.Exception != null)
{
Debug.LogWarning(message: $"Failed to register task with {DBTask.Exception}");
}
else
{
//Kills are now updated
}
}
And This is Where I Retrieve the data from Firebase Realtime Database.
private IEnumerator LoadUserData()
{
//Get the currently logged in user data
var DBTask = DBreference.Child("users").Child(User.UserId).GetValueAsync();
yield return new WaitUntil(predicate: () => DBTask.IsCompleted);
if (DBTask.Exception != null)
{
Debug.LogWarning(message: $"Failed to register task with {DBTask.Exception}");
}
else if (DBTask.Result.Value == null)
{
//No data exists yet
xpField.text = "0";
killsField.text = "0";
deathsField.text = "0";
}
else
{
//Data has been retrieved
DataSnapshot snapshot = DBTask.Result;
xpField.text = snapshot.Child("xp").Value.ToString();
killsField.text = snapshot.Child("kills").Value.ToString();
deathsField.text = snapshot.Child("deaths").Value.ToString();
}
}
It works perfectly, but the only problem is that it replacing the Score instead of updating it or start from where I left. Any help would be appreciated. Thank You.
Your answer
Follow this Question
Related Questions
Security with android 1 Answer
POST request to RESTful APIs 0 Answers
WWWForm and xamp??? 0 Answers
Best Practice: Game Data Human in Readable Format 0 Answers
Database and connection with unity Steam vs MySql vs SomethingElse (MultiPlayer) 0 Answers