Andrés Heredia
Andrés Heredia

Reputation: 21

Unity Firebase [to Android] error when using database and auth package in one project

I'm developing an app for Android in Unity3d that uses Firebase Authentication, and it worked just fine until I tried to use the Firebase database package. It imported just fine. But when I typed code to set my database in the Start() function for my app such as FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://YOUR-Firebase-APP.Firebaseio.com/"); (which is in the Firebase unity guide https://Firebase.google.com/docs/database/unity/start). it crashes almost every time I use authentication or database functions, or a few seconds after.

This is my start() function before I added the database package:

Firebase.Auth.FirebaseAuth auth;
Firebase.Auth.FirebaseUser user;
Firebase.DependencyStatus dependencyStatus = Firebase.DependencyStatus.UnavailableOther;

    void Start () {
        dependencyStatus = Firebase.FirebaseApp.CheckDependencies();
        if (dependencyStatus != Firebase.DependencyStatus.Available) {
            Firebase.FirebaseApp.FixDependenciesAsync().ContinueWith(task => {
                dependencyStatus = Firebase.FirebaseApp.CheckDependencies();
                if (dependencyStatus == Firebase.DependencyStatus.Available) {
                    InitializeFirebase();
                } else {
                    // This should never happen if we're only using Firebase Analytics.
                    // It does not rely on any external dependencies.
                    Debug.LogError(
                        "Could not resolve all Firebase dependencies: " + dependencyStatus);
                }
            });
        } else {
            InitializeFirebase();
        }
}void InitializeFirebase() {
        Debug.Log("Setting up Firebase Auth");
        auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
        auth.StateChanged += AuthStateChanged;
    }
    // Track state changes of the auth object.
    void AuthStateChanged(object sender, System.EventArgs eventArgs) {
        if (auth.CurrentUser != user) {
            if (user == null && auth.CurrentUser != null) {
                Debug.Log("Signed in " + auth.CurrentUser.DisplayName);
            } else if (user != null && auth.CurrentUser == null) {
                Debug.Log("Signed out " + user.DisplayName);
            }
            user = auth.CurrentUser;
            //Debug.Log("Signed in " + auth.CurrentUser.DisplayName);

        }
    }

And this is my code after I applied the database connection (I just added a variable at the beginning and few lines in the InitializeFirebase() Function):

Firebase.Auth.FirebaseAuth auth;
Firebase.Auth.FirebaseUser user;
DatabaseReference mRef;
Firebase.DependencyStatus dependencyStatus = Firebase.DependencyStatus.UnavailableOther;

    void Start () {
        
        
        dependencyStatus = Firebase.FirebaseApp.CheckDependencies();
        if (dependencyStatus != Firebase.DependencyStatus.Available) {
            Firebase.FirebaseApp.FixDependenciesAsync().ContinueWith(task => {
                dependencyStatus = Firebase.FirebaseApp.CheckDependencies();
                if (dependencyStatus == Firebase.DependencyStatus.Available) {
                    InitializeFirebase();
                } else {
                    // This should never happen if we're only using Firebase Analytics.
                    // It does not rely on any external dependencies.
                    Debug.LogError(
                        "Could not resolve all Firebase dependencies: " + dependencyStatus);
                }
            });
        } else {
            InitializeFirebase();
        }
void InitializeFirebase() {
        Debug.Log("Setting up Firebase Auth");
        auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
        auth.StateChanged += AuthStateChanged;
        FirebaseApp app = FirebaseApp.DefaultInstance;
        app.SetEditorDatabaseUrl("https://testapp-509c4.firebaseio.com/");
        mRef = FirebaseDatabase.DefaultInstance.RootReference;
    }
    // Track state changes of the auth object.
    void AuthStateChanged(object sender, System.EventArgs eventArgs) {
        if (auth.CurrentUser != user) {
            if (user == null && auth.CurrentUser != null) {
                Debug.Log("Signed in " + auth.CurrentUser.DisplayName);
            } else if (user != null && auth.CurrentUser == null) {
                Debug.Log("Signed out " + user.DisplayName);
            }
            user = auth.CurrentUser;
            //Debug.Log("Signed in " + auth.CurrentUser.DisplayName);

        }
    }

I'm not sure what the problem is. Maybe both instances try to access the same permission or instance. I get this error on the log every time I try functions like auth.CreateUserWithEmailAndPasswordAsync(email, password) OR italicauth.SignInWithEmailAndPasswordAsync(username_text,password_text) which worked fine before I try to connect to firebase database

The Android logcat shows this every time I do one of those functions (even if it crashes or not):

12-01 00:55:51.214 4615-4639/? I/Unity: NullReferenceException: Object reference not set to an instance of an object
    at Intro.Login () [0x00000] in <filename unknown>:0 
    at UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) [0x00000] in <filename unknown>:0 
    at UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) [0x00000] in <filename unknown>:0 
    at UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) [0x00000] in <filename unknown>:0 
    at UnityEngine.Events.UnityEvent.Invoke () [0x00000] in <filename unknown>:0 
    at UnityEngine.UI.Button.Press () [0x00000] in <filename unknown>:0 
    at UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) [0x00000] in <filename unknown>:0 
    at UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) [0x00000] in <filename unknown>:0 
    at UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngi
12-01 00:55:51.224 21998-22429/? E/AsyncOperation: serviceID=16, operation=ValidateAuthServiceOperation
    java.lang.NullPointerException: onPostInitComplete can be called only once per call to getRemoteService
        at iri.a(:com.google.android.gms:74)
        at ioj.a(:com.google.android.gms:987)
        at ipf.a(:com.google.android.gms:66)
        at ixg.a(:com.google.android.gms:284)
        at eks.a(:com.google.android.gms:125)
        at eks.a(:com.google.android.gms:113)
        at ixn.run(:com.google.android.gms:113)
        at jaq.run(:com.google.android.gms:450)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at jew.run(:com.google.android.gms:17)
        at java.lang.Thread.run(Thread.java:841)

Please help, if you need more code from my project just let me know. Thank You

Upvotes: 2

Views: 3325

Answers (3)

Atul Patel
Atul Patel

Reputation: 81

Firebase Unity Latest Version Deprecated SetEditorDatabaseUrl

string  BaseURL = "https://yourapp-e756f.firebaseio.com/";

FirebaseApp.DefaultInstance.SetEditorDatabaseUrl(BaseURL ); //Deprecated
   
//please comment the code.

Upvotes: 0

Jesse de gans
Jesse de gans

Reputation: 1654

It's missing the firebase unity editor reference. Try adding:

using Firebase.Unity.Editor;

Upvotes: 1

LTB
LTB

Reputation: 11

I think FirebaseApp.DefaultInstance.SetEditorDatabaseUrl is destined for the unity editor so Android you should not have that included.

Try wrapping it like this:

#if UNITY_EDITOR
  FirebaseApp.DefaultInstance.SetEditorDatabaseUrl
#endif

Upvotes: 1

Related Questions