- Home /
Firebase - GetValueAsync() Returning Inconsistent Values
Hello, I've been stumped with this issue for a minute now, and I decided to ask for help. When using the firebase sdk, when I call GetValueAsync, the value it returns is different every time I start the game. Let's look at the important code:
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using Firebase; using Firebase.Database; using Firebase.Auth; public class LeaderboardManagerScript : MonoBehaviour { private FirebaseDatabase database; private DatabaseReference reference; public string email; public int coins; private IEnumerator logInCoroutine; void Start() { database = FirebaseDatabase.DefaultInstance; reference = database.RootReference; var auth = FirebaseAuth.DefaultInstance; var currentUser = auth.CurrentUser; if (currentUser == null) { email = ""; coins = 0; } else { logInCoroutine = logIn(); StartCoroutine(logInCoroutine); } } private IEnumerator logIn() { var auth = FirebaseAuth.DefaultInstance; var currentUser = auth.CurrentUser; email = currentUser.Email; email = email.Replace("@", "at"); email = email.Replace(".", "dot"); var dataTask = reference.Child("users").Child(email).GetValueAsync(); yield return new WaitUntil(() => dataTask.IsCompleted); DataSnapshot displayNameSnapshot = dataTask.Result.Child("displayName"); DataSnapshot coinsSnapshot = dataTask.Result.Child("coins"); coins = Convert.ToInt32(coinsSnapshot.Value.ToString()); } public void updateCoins() { var auth = FirebaseAuth.DefaultInstance; var currentUser = auth.CurrentUser; if (currentUser != null) { email = currentUser.Email; email = email.Replace("@", "at"); email = email.Replace(".", "dot"); reference.Child("users").Child(email).Child("coins").SetValueAsync(coins); } } }
Note: the coins variable is updated by another script, and the call for updateCoins() is as well. It usually works perfectly; however, about 1 in 5 times that I playtest it in the Unity editor, it sets coins equal to a previous value. My guess is it has to do with the asynchronous setup I'm using, but when I tried using ContinueWith instead, I got the exact same results. Maybe it is a flaw of the Unity playtester? Anyway, any help would be appreciated.
Your answer
![](https://koobas.hobune.stream/wayback/20220613021712im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do I create an database to store Question and answers 1 Answer
get_version can only be called from the main thread. 0 Answers
Callback function cannot change game object status,callback function cannot change gameobject status 0 Answers