Question: using System.Collections; using System.Collections.Generic; using UnityEngine; using Firebase; using Firebase.Database; using UnityEngine.UI; using TMPro; public class DBManager : MonoBehaviour { / / Firebase references public

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Firebase;
using Firebase.Database;
using UnityEngine.UI;
using TMPro;
public class DBManager : MonoBehaviour
{
// Firebase references
public DatabaseReference usersRef;
// UI input fields
public TMP_InputField usernameInput;
public TMP_InputField passwordInput;
void Start()
{
// Initialize Firebase asynchronously
StartCoroutine(InitializeFirebase());
}
private IEnumerator InitializeFirebase()
{
var task = FirebaseApp.CheckAndFixDependenciesAsync();
yield return new WaitUntil(()=> task.IsCompleted);
if (task.IsCanceled || task.IsFaulted)
{
Debug.LogError("Firebase initialization error: "+ task.Exception);
yield break;
}
else
{
// Firebase initialization successful
usersRef = FirebaseDatabase.DefaultInstance.GetReference("Users");
Debug.Log("Firebase initialization completed");
}
}
// Save user data to Firebase
public void SaveUser()
{
string username = usernameInput.text;
string password = passwordInput.text;
Dictionary user = new Dictionary();
user["username"]= username;
user["password"]= password;
string key = usersRef.Push().Key;
usersRef.Child(key).UpdateChildrenAsync(user);
}
// Get user data from Firebase
public void GetData()
{
if (string.IsNullOrEmpty(usernameInput.text))
{
Debug.LogWarning("Username field is empty");
return;
}
StartCoroutine(GetUserData());
}
private IEnumerator GetUserData()
{
string username = usernameInput.text;
var task = usersRef.Child(username).GetValueAsync();
yield return new WaitUntil(()=> task.IsCompleted);
if (task.IsCanceled || task.IsFaulted)
{
Debug.LogError("Firebase data retrieval error: "+ task.Exception);
yield break;
}
DataSnapshot snapshot = task.Result;
if (!snapshot.Exists)
{
Debug.LogWarning("User data does not exist for the specified username.");
yield break;
}
foreach (DataSnapshot user in snapshot.Children)
{
if (user.Key == "password")
{
Debug.Log("Password: "+ user.Value.ToString());
}
else if (user.Key == "username")
{
Debug.Log("Username: "+ user.Value.ToString());
}
}
}
}User data does not exist for the specified username. error

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!