title: Save and load data to firebase not working unity
body: <p>maybe my code will mess here beacuse I'm new into firebase stuff and there is lack doc for newbie like me, so I want to make save and load data from firebase, there is 2 scene the first for <code>main menu</code> and second one the <code>main game</code>,so when firstime login or auto login, user will load <code>current user</code> data from realtime database (<code>I'm using firebase</code>)  also applies to user when back to main menu.</p>
  <p>in the second scene itself, user load current user highscore data at first start then when game over if the <code>score &gt; lasthighscore</code> that will save/update latest highscore to realtime database.</p>
  <p>here the preview this is when I auto-login then playgame until game over:
  <img src="https://media.giphy.com/media/4ZTiwtXCT2wHkPPvYE/giphy.gif" alt="" /></p>
  <p>it's supposed to be updated but not when back to main menu, I already try with player prefs since i erase all data when sign out so the highscore always be 0 and if login with other account that highscore is not belong to other account.
  here my firebase script on main menu:</p>
  <pre class="lang-cs prettyprint-override"><code>   private void Start()
      {
          Time.timeScale = 1;
          mainPanel.SetActive(true);
          InitializeFirebase();
      }
  
      void InitializeFirebase()
      {
          auth = FirebaseAuth.DefaultInstance;
          DBreference = FirebaseDatabase.DefaultInstance.RootReference;
          auth.StateChanged += AuthStateChanged;
          AuthStateChanged(this, null);
      }
  
      void AuthStateChanged(object sender, System.EventArgs eventArgs)
      {
          //This checks if the user (your local user) is the same as the one from the auth
          if (auth.CurrentUser != User)
          {
              //this seems the same, but user could have been null before
              bool signedIn = User != auth.CurrentUser &amp;&amp; auth.CurrentUser != null;
              if (!signedIn &amp;&amp; User != null)
              {
                  Debug.Log(&quot;Signed out &quot; + User.UserId);
                  loginPanel.SetActive(true);
              }
              //this is important step, this user is the one you should be working with
              User = auth.CurrentUser;
              if (signedIn)
              {
                  Debug.Log(&quot;Signed in &quot; + User.UserId);
                  userNameShowText.text = User.DisplayName;
                  StartCoroutine(LoadUserData());
                  loginPanel.SetActive(false);
  
                  //            //highScoreMainMenu.text = PlayerPrefs.GetInt(&quot;highscore&quot;).ToString(&quot;0000000&quot;);
  
              }
              else
              {
                  loginPanel.SetActive(true);
              }
          }
      }
      
       public IEnumerator Login(string _email, string _password)
      {
          //Call the Firebase auth signin function passing the email and password
          var LoginTask = auth.SignInWithEmailAndPasswordAsync(_email, _password);
          //Wait until the task completes
          yield return new WaitUntil(predicate: () =&gt; LoginTask.IsCompleted);
  
          if (LoginTask.Exception != null)
          {
              //If there are errors handle them
              Debug.LogWarning(message: $&quot;Failed to register task with {LoginTask.Exception}&quot;);
              FirebaseException firebaseEx = LoginTask.Exception.GetBaseException() as FirebaseException;
              AuthError errorCode = (AuthError)firebaseEx.ErrorCode;
  
              string message = &quot;Login Failed!&quot;;
              switch (errorCode)
              {
                  case AuthError.MissingEmail:
                      message = &quot;Missing Email&quot;;
                      break;
                  case AuthError.MissingPassword:
                      message = &quot;Missing Password&quot;;
                      break;
                  case AuthError.WrongPassword:
                      message = &quot;Wrong Password&quot;;
                      break;
                  case AuthError.InvalidEmail:
                      message = &quot;Invalid Email&quot;;
                      break;
                  case AuthError.UserNotFound:
                      message = &quot;Account does not exist&quot;;
                      break;
              }
              warningLoginText.text = message;
          }
          else
          {
              //User is now logged in
              //Now get the result
              User = LoginTask.Result;
              Debug.LogFormat(&quot;User signed in successfully: {0} ({1})&quot;, User.DisplayName, User.Email);
              warningLoginText.text = &quot;&quot;;
              confirmLoginText.text = &quot;Logged In&quot;;
  
              StartCoroutine(LoadUserData());
  
              yield return new WaitForSeconds(2);
  
              userNameShowText.text = User.DisplayName;
              UserDataScreen(); // Change to user data UI
              confirmLoginText.text = &quot;&quot;;
              ClearLoginFeilds();
              ClearRegisterFeilds();
          }
      }
  
       public IEnumerator UpdateHighScore(int _highScore)
      {
          var DBTask = DBreference.Child(&quot;users&quot;).Child(auth.CurrentUser.UserId).Child(&quot;highscore&quot;).SetValueAsync(_highScore);
  
          yield return new WaitUntil(predicate: () =&gt; DBTask.IsCompleted);
  
          if (DBTask.Exception != null)
          {
              Debug.LogWarning(message: $&quot;Failed to register task with {DBTask.Exception}&quot;);
          }
          else
          {
             //giscrore now updated
          }
      }
  
  </code></pre>
  <p>and this is for the game scene also same function for update higscore like on main menu.</p>
  <pre class="lang-cs prettyprint-override"><code>void Start()
      {
          InitializeFirebase();
  
          YetGameOver();
          //GetScore
          score = 0;
          scoreText.text = score.ToString(&quot;00000&quot;);
          //highScoreText.text = &quot;HI :&quot; + PlayerPrefs.GetInt(&quot;highscore&quot;, 0).ToString(&quot;00000&quot;);
          maxTime = .1f;
      }
  
     void InitializeFirebase()
      {
          auth = FirebaseAuth.DefaultInstance;
          DBreference = FirebaseDatabase.DefaultInstance.RootReference;
          auth.StateChanged += AuthStateChanged;
          AuthStateChanged(this, null);
      }
  
      void AuthStateChanged(object sender, System.EventArgs eventArgs)
      {
          //This checks if the user (your local user) is the same as the one from the auth
          if (auth.CurrentUser != User)
          {
              //this seems the same, but user could have been null before
              bool signedIn = User != auth.CurrentUser &amp;&amp; auth.CurrentUser != null;
              if (!signedIn &amp;&amp; User != null)
              {
                  Debug.Log(&quot;Signed out &quot; + User.UserId);
              }
              //this is important step, this user is the one you should be working with
              User = auth.CurrentUser;
              if (signedIn)
              {
                  Debug.Log(&quot;Signed in &quot; + User.UserId);
                  StartCoroutine(LoadUserData());
                  //highScoreMainMenu.text = PlayerPrefs.GetInt(&quot;highscore&quot;).ToString(&quot;0000000&quot;);
  
              }
          }
      }
  
    public void GameOver()
      {
          SaveData();
          StartCoroutine(WaitToDeath());
      }
  
  public void SaveData()
      {
          Debug.Log(&quot;Saved&quot;);
          StartCoroutine(UpdateHighScore(PlayerPrefs.GetInt(&quot;highscore&quot;, 0)));
      }
  
   public IEnumerator WaitToDeath()
      {
  
          _CamShake.instance.shouldShake = true;
  
          DeathSound();
  
          gameOverPanel.SetActive(true);
  
          endHighScoreText.text = &quot;HI :&quot; + PlayerPrefs.GetInt(&quot;highscore&quot;).ToString(&quot;0000000&quot;);
          scoreText.gameObject.SetActive(false);
          highScoreText.gameObject.SetActive(false);
  
          yield return new WaitForSeconds(.1f);
  
          //_AdmobAds.instance.ShowInterstitialAd();
          isStarted = false;
          isGameOver = true;
          Time.timeScale = 0;
      }
  //same function like main menu
  public IEnumerator UpdateHighScore(int _highScore)
      {
          var DBTask = DBreference.Child(&quot;users&quot;).Child(auth.CurrentUser.UserId).Child(&quot;highscore&quot;).SetValueAsync(_highScore);
  
          yield return new WaitUntil(predicate: () =&gt; DBTask.IsCompleted);
  
          if (DBTask.Exception != null)
          {
              Debug.LogWarning(message: $&quot;Failed to register task with {DBTask.Exception}&quot;);
          }
          else
          {
              //highscore are now updated
              
          }
      }
  
  public IEnumerator LoadUserData()
      {
          //Get the currently logged in user data
          var DBTask = DBreference.Child(&quot;users&quot;).Child(User.UserId).GetValueAsync();
  
          yield return new WaitUntil(predicate: () =&gt; DBTask.IsCompleted);
  
          if (DBTask.Exception != null)
          {
              Debug.LogWarning(message: $&quot;Failed to register task with {DBTask.Exception}&quot;);
          }
          else if (DBTask.Result.Value == null)
          {
              //No data exists yet
              highScoreText.text = &quot;0&quot;;
              endHighScoreText.text = &quot;0&quot;;
          }
          else
          {
              //Data has been retrieved
              DataSnapshot snapshot = DBTask.Result;
              highScoreText.text = endHighScoreText.text = snapshot.Child(&quot;highscore&quot;).Value.ToString();
          }
      }
  
  </code></pre>