Search Unity

Question firebase database unity problem

Discussion in 'Scripting' started by MonnalSimulator, Mar 16, 2023.

  1. MonnalSimulator

    MonnalSimulator

    Joined:
    Sep 15, 2020
    Posts:
    2
    i really don't know what i am doing wrong, i made another class names User and stored a string called username in it, and then on another script put this code, so i can save database in firebase, it says that the line (DBreference.Child...) is the line that is causing the error and it is object reference not set to an instance

    Code (CSharp):
    1. public void SaveData()
    2.     {
    3.         User user = new User();
    4.         user.Username = usernameRegisterField.text;
    5.         string json = JsonUtility.ToJson(user);
    6.         DBreference.Child("User").Child(user.Username).SetRawJsonValueAsync(json).ContinueWith(task =>
    7.         {
    8.             if (task.IsCompleted)
    9.             {
    10.                 Debug.Log("successfully added data");
    11.             }
    12.             else
    13.             {
    14.                 Debug.Log("not successfull");
    15.             }
    16.         });
    17.     }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    It's a standard null ref error.
    So, start debugging your values and see what is null. Is DBreference null?

    Also, I suggest always using ContinueWithOnMainThread instead of just ContinueWith.
     
  3. MonnalSimulator

    MonnalSimulator

    Joined:
    Sep 15, 2020
    Posts:
    2
    Thank you so much for the response! i will try to fix it