Unity中Firebase手机认证代码报错求助:无法识别codeAutoRetrievalTimeout
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
"cannot resolve symbol codeAutoRetrievalTimeout"
This happens because the version of Firebase Unity SDK you're using might not support thecodeAutoRetrievalTimeoutnamed parameter in the overload ofVerifyPhoneNumberyou're calling. Some older SDK versions don't include this callback, or the method signature uses a different structure for passing callbacks."redundant name identifier"
When you removed the problematic parameter, you kept using named parameters likeverificationCompleted:— 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 Cacheand reimport your Firebase packages if you still run into issues.
内容的提问来源于stack exchange,提问作者Yavuz Koca

