'Unity Firebase Null reference problem. After firebase authentication cannot send or recieve data in unity 2020.1.17f
Here is the code i am currently using to send data and receive data: all the plugins have been downloaded to the latest version.we are trying to use firebase real time database is send and receive scores that have to be implemented in the game.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Firebase.Auth;
using Firebase;
using Google;
using Firebase.Database;
using System.Linq;
public class LBnew : MonoBehaviour
{
private GoogleSignInConfiguration configuration;
private FirebaseAuth auth;
public string userID;
private DatabaseReference reference;
public string webClientId = " <your client id>";
private void Awake()
{
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
{
var dependencyStatus = task.Result;
if (dependencyStatus == DependencyStatus.Available)
{
FirebaseApp app;
app = FirebaseApp.DefaultInstance;
Initialize();
}
else
{
Debug.Log(" not resolve firebase dependencies");
}
});
}
void Initialize()
{
configuration = new GoogleSignInConfiguration { WebClientId = webClientId, RequestEmail = true, RequestIdToken = true };
auth = FirebaseAuth.DefaultInstance;
userID = FirebaseAuth.DefaultInstance.CurrentUser.DisplayName;
reference = FirebaseDatabase.DefaultInstance.RootReference;
Debug.Log(" initiallized");
sendscore();
}
public void sendscore()
{
StartCoroutine(UpdateScore(10));
}
private IEnumerator UpdateScore(int _score)
{
Debug.Log(" Your userID " + userID);
//Set the currently logged in user score
var DBTask = reference.Child("user").Child(userID).Child("score").SetValueAsync(_score);
yield return new WaitUntil(predicate: () => DBTask.IsCompleted);
if (DBTask.Exception != null)
{
Debug.LogWarning(message: $"Failed to register score task with {DBTask.Exception}");
// Debug.Log(" failed to reg score ");
}
else
{
Debug.Log(" success to reg score ");
}
StartCoroutine(LoadScoreboardData());
}
private IEnumerator LoadScoreboardData()
{
var DBTask = reference.Child("users").OrderByChild("score").GetValueAsync();
yield return new WaitUntil(predicate: () => DBTask.IsCompleted);
if (DBTask.Exception != null)
{
Debug.LogWarning(message: $" load leaderboard task with {DBTask.Exception}");
Debug.Log(" srinu failed to load leaderScoreboardData ");
}
else
{
//Data has been retrieved
DataSnapshot snapshot = DBTask.Result;
//Destroy any existing scoreboard elements
//foreach (Transform child in scoreboardContent.transform)
//{
// Destroy(child.gameObject);
//}
//Loop through every users UID
//foreach (DataSnapshot childSnapshot in snapshot.Children)
//{
foreach (DataSnapshot childSnapshot in snapshot.Children.Reverse<DataSnapshot>())
{
// Instantiate new scoreboard elements
// GameObject scoreboardElement = Instantiate(scoreElement, scoreboardContent);
// scoreboardElement.GetComponent<LbAllList>().NewDataElement(username, Schoolname, score);
Debug.Log(" leaderScoreboardData " + childSnapshot.Child("score"));
}
//}
}
}
}
we are using unity 2020.1.17f as the version of unity Any help would be appreciated thanks
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
