I followed all steps such as adding the database SDK to Unity etc., but when I try to connect Unity to Firebase, I'm getting an error.
Assets\Script\RealTimeLoading.cs(14,21): error CS0029: Cannot implicitly convert type 'Firebase.Database.DatabaseReference' to 'DatabaseReference'
I followed a lot of tutorials, they are never getting errors, just me. Here is my code
using Firebase;
using Firebase.Database;
using Firebase.Unity.Editor;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RealTimeLoading : MonoBehaviour
{
DatabaseReference reference;
// Start is called before the first frame update
void Start()
{
reference = FirebaseDatabase.DefaultInstance.RootReference;
}
// Update is called once per frame
void Update()
{
}
}
The error is on this line:
FirebaseDatabase.DefaultInstance.RootReference;
What did I do wrong?
After briefly going through your using statements, I don't believe that there should be a DatabaseReference other than the one in Firebase.Database. That makes me think that you have another class in your project's root namespace with the same name.
First, I'd recommend removing Firebase.Unity.Editor and definitely let me know if there's still some documentation recommending it.
Then you should be able to simply write:
using DatabaseReference = Firebase.Database.DatabaseReference;
with your other using directives. So the top of your file may look like:
using Firebase;
using Firebase.Database;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DatabaseReference = Firebase.Database.DatabaseReference;
Be careful with this because if there is another class named DatabaseReference in your code base, you may run into this naming conflict more often. It could be beneficial to either move it to its own namespace or to rename it if possible. But this should get you unstuck immediately.
Alternatively: instead of adding the using alias, you may instead fully qualify the name in your class. For example:
using Firebase;
using Firebase.Database;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RealTimeLoading : MonoBehaviour
{
Firebase.Database.DatabaseReference reference;
// Start is called before the first frame update
void Start()
{
reference = FirebaseDatabase.DefaultInstance.RootReference;
}
}
I would recommend against doing both of these in one file, but you may have to use each under different circumstances.