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

Unity中Firebase手机认证代码报错求助:无法识别codeAutoRetrievalTimeout

Unity Firebase Phone Auth: Fixing "cannot resolve symbol codeAutoRetrievalTimeout" & "redundant name identifier" Errors

Let's break down what's going wrong with your Firebase phone auth code in Unity and how to fix it.

Why You're Seeing These Errors

  1. "cannot resolve symbol codeAutoRetrievalTimeout"
    This happens because the version of Firebase Unity SDK you're using might not support the codeAutoRetrievalTimeout named parameter in the overload of VerifyPhoneNumber you're calling. Some older SDK versions don't include this callback, or the method signature uses a different structure for passing callbacks.

  2. "redundant name identifier"
    When you removed the problematic parameter, you kept using named parameters like verificationCompleted: — but the overload you ended up targeting doesn't expect named parameters here, or the parameter names don't match exactly. C# throws this error when it can't map your named arguments to the method's signature.

The Fix: Use the Correct Method Overload

The cleanest way to avoid these issues is to use the PhoneAuthOptions builder pattern, which is the recommended approach in newer Firebase SDK versions. It makes your code more readable and avoids signature mismatches. Here's the corrected code:

public void sendSms() 
{ 
    Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance; 
    string phoneNumber = "+905555555555"; 
    uint phoneAuthTimeoutMs = 10000; 

    PhoneAuthProvider provider = PhoneAuthProvider.GetInstance(auth); 

    // Build verification options with all required callbacks
    var verificationOptions = new PhoneAuthOptions.Builder(auth)
        .SetPhoneNumber(phoneNumber)
        .SetTimeout(phoneAuthTimeoutMs, TimeUnit.MILLISECONDS)
        .SetCallbacks(new PhoneAuthProvider.OnVerificationStateChangedCallbacks 
        {
            OnVerificationCompleted = (credential) => 
            {
                // Handle auto-verification success (e.g., instant verification on some devices)
            },
            OnVerificationFailed = (error) => 
            {
                // Handle verification failures (invalid number, network issues, etc.)
                Debug.LogError($"Verification failed: {error.Message}");
            },
            OnCodeSent = (verificationId, forceResendingToken) => 
            {
                MyText.text = "SMS Has been sent " + verificationId;
            },
            OnCodeAutoRetrievalTimeout = (verificationId) => 
            {
                // Handle timeout if the code wasn't auto-retrieved within the set time
            }
        })
        .Build();

    // Trigger the phone number verification
    provider.VerifyPhoneNumber(verificationOptions);

    MyText.text += "HMM"; 
}

If you prefer using the direct method call (without the builder), make sure you're using the correct callback types and avoiding named parameters that don't match your SDK version:

public void sendSms() 
{ 
    Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance; 
    string phoneNumber = "+905555555555"; 
    uint phoneAuthTimeoutMs = 10000; 

    PhoneAuthProvider provider = PhoneAuthProvider.GetInstance(auth); 
    provider.VerifyPhoneNumber(
        phoneNumber, 
        phoneAuthTimeoutMs, 
        null, // ForceResendingToken (pass a token here if you need to resend SMS)
        // Verification completed callback
        new PhoneAuthProvider.OnVerificationCompletedCallback((credential) => { }), 
        // Verification failed callback
        new PhoneAuthProvider.OnVerificationFailedCallback((error) => { }), 
        // Code sent callback
        new PhoneAuthProvider.OnCodeSentCallback((id, token) => { 
            MyText.text = "SMS Has been sent " + id; 
        }), 
        // Auto-retrieval timeout callback
        new PhoneAuthProvider.OnCodeAutoRetrievalTimeoutCallback((id) => { })
    ); 

    MyText.text += "HMM"; 
}

Additional Tips to Avoid Future Issues

  • Update Firebase SDK: Make sure you're using the latest stable version of the Firebase Unity SDK (check via Unity Package Manager or the Firebase Console). Older versions often have outdated method signatures that don't match the latest docs.
  • Check Unity-Specific Docs: General Firebase docs might reference web/Android/iOS APIs that don't exactly match Unity's implementation. Always refer to Firebase's official Unity SDK documentation for accurate method signatures.
  • Clear Unity Cache: Sometimes stale cache can cause API mismatch errors. Go to Assets > Clear Cache and reimport your Firebase packages if you still run into issues.

内容的提问来源于stack exchange,提问作者Yavuz Koca

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:22:36