- Home /
FirebaseDatabase.DefaultInstance.RootReference == null
Hello everyone,
I am actually trying to get my application working together with a Firebase Realtime Database. For some time getting the reference to the database worked fine. It is actually a quite simple line:
FirebaseDatabase.DefaultInstance.RootReference
But now it returns null and I do not have an idea why?!
private void InitializeFirebase()
{
Debug.Log("Initializing FirebaseManagerGame + DB");
try
{
DBreference = FirebaseDatabase.DefaultInstance.RootReference;
}
catch (Exception e)
{
Debug.Log("Gotacha! " + e.Message);
if (DBreference == null)
Debug.Log("DBreference is null");
throw e;
}
Output:
Gotacha! Object reference not set to an instance of an object
UnityEngine.Debug:Log (object)
FirebaseManagerGame:InitializeFirebase () (at Assets/Scripts/FirebaseManagerGame.cs:72)
FirebaseManagerGame:<Awake>b__4_0 (System.Threading.Tasks.Task`1<Firebase.DependencyStatus>) (at Assets/Scripts/FirebaseManagerGame.cs:44)
System.Threading._ThreadPoolWaitCallback:PerformWaitCallback ()
DBreference is null
Do you have any idea why?
By the way: WIthout the try catch block I do not see any exception. It simply does not work and the exception is hidden, which does not really make it easy to find the problem...
How did you initialize the FirebaseApp instance? Did you use the config file from Firebase?
[EDIT: Problem solved see other reply]
Yes, indeed. I got the google-services.json from my Firebase project and added it to my Asset folder.
Then adding:
using Firebase; using Firebase.Database;
finally calling the InitializeFirebase() method above within the awake method:
void Awake()
{
//Check that all of the necessary dependencies for Firebase are present on the system
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
{
dependencyStatus = task.Result;
if (dependencyStatus == DependencyStatus.Available)
{
//If they are avalible Initialize Firebase
InitializeFirebase();
}
else
{
Debug.LogError("Could not resolve all Firebase dependencies: " + dependencyStatus);
}
Answer by XoetziX · Jun 06, 2021 at 05:43 PM
I just solved the problem, but I do not understand why... Hopefully anyone can explain it to me so that I can avoid similar problems in the future.
I used an empty game object and attached my 2 FirebaseManager scripts. The ...Auth script is only needed at my GameMenu scene, so I divided them.
It turned out that attaching both scripts to one game object causes the curious problem mentioned above.
I solved it by creating another game object and attaching one of the 2 scripts to the new object so that at the end I have 2 game objects with each 1 script attached.
Are there maybe any competing actions taking place if both scripts belong to one game object?
Your answer
Follow this Question
Related Questions
Array containing destroyed object = Null? 1 Answer
Find if no objects with tag exist 3 Answers
Checking for null elements in a subset? 1 Answer
How do I stop a function from executing? 1 Answer
Check if Array.GetValue(i) is null 1 Answer