You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity集成Firebase Authentication报错:对象引用未设置到对象实例

Unity Firebase Authentication 初始化与登录报错排查

完全按照官方文档复制代码,运行游戏时在Login方法的auth.SignInWithEmailAndPasswordAsync调用处报错,推测问题出在InitializeFirebase的auth初始化环节,但无法定位解决。

相关代码

Login方法

private void Login(string _email, string _password)
{
    Debug.Log(_email);
    Debug.Log(_password);
    //Call the Firebase auth signin function passing the email and password
    auth.SignInWithEmailAndPasswordAsync(_email, _password).ContinueWith(task =>    //error line
    {
        if (task.IsCanceled)
        {
            Debug.Log("Task Canceled");
            return;
        }
        if (task.IsFaulted)
        {
            Debug.Log(task.Exception);
            return;
        }

        Firebase.Auth.FirebaseUser newUser = task.Result;
        Debug.LogFormat("Sign in done",
        newUser.DisplayName, newUser.UserId);
    });
    //Wait until the task completes
    
}

初始化函数

void InitializeFirebase()
{
    auth = FirebaseAuth.DefaultInstance;
    auth.StateChanged += AuthStateChanged;
    AuthStateChanged(this, null);
}

排查与解决步骤

  • 确认Firebase SDK配置正确性

    • 检查Unity Package Manager中Firebase Auth包的版本,确保和项目中其他Firebase组件(如Firebase Core)版本一致,避免版本冲突。
    • 确认google-services.json(Android)或GoogleService-Info.plist(iOS)已正确放置在Assets根目录,且文件内的项目ID、API密钥等信息与Firebase控制台的项目匹配。
  • 修正初始化时机与逻辑
    Firebase Auth的初始化必须依赖FirebaseApp完成初始化,修改InitializeFirebase方法,先等待依赖检查完成:

    async void InitializeFirebase()
    {
        var dependencyStatus = await FirebaseApp.CheckAndFixDependenciesAsync();
        if (dependencyStatus == DependencyStatus.Available)
        {
            auth = FirebaseAuth.DefaultInstance;
            auth.StateChanged += AuthStateChanged;
            AuthStateChanged(this, null);
        }
        else
        {
            Debug.LogError($"Firebase依赖初始化失败: {dependencyStatus}");
        }
    }
    
  • 添加空引用检查
    在Login方法开头增加auth的空判断,提前排查未初始化问题:

    private void Login(string _email, string _password)
    {
        if (auth == null)
        {
            Debug.LogError("Firebase Auth未初始化,请先调用InitializeFirebase");
            return;
        }
        // 原有登录逻辑...
    }
    
  • 获取详细错误信息
    优化task.IsFaulted分支的日志输出,获取具体错误码和描述,方便定位问题:

    if (task.IsFaulted)
    {
        foreach (var exception in task.Exception.InnerExceptions)
        {
            Firebase.FirebaseException firebaseEx = exception as Firebase.FirebaseException;
            if (firebaseEx != null)
            {
                var errorCode = (Firebase.Auth.AuthError)firebaseEx.ErrorCode;
                Debug.LogError($"登录错误: {errorCode} - {firebaseEx.Message}");
            }
            else
            {
                Debug.LogError($"登录异常: {exception.Message}");
            }
        }
        return;
    }
    
  • 处理线程切换问题
    Unity的API仅能在主线程调用,使用ContinueWithOnMainThread替代ContinueWith(需导入Firebase.Extensions包),避免后续操作因线程问题报错:

    auth.SignInWithEmailAndPasswordAsync(_email, _password).ContinueWithOnMainThread(task =>
    {
        // 原有登录逻辑...
    });
    

内容的提问来源于stack exchange,提问作者Laviz Pandey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 07:00:58