I have created a simple Email/password authentication system using Firebase.
Everything works fine until I successfully log in and get all the expected logs in the console, then the script is supposed to taken me to another scene via a simple method.
The method seems not to work correctly since I’m not being taken to the expected scene but I do see the log from that method.
Any ideas?
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;
using Firebase;
using Firebase.Auth;
using Firebase.Firestore;
using System.Collections.Generic;
public class AuthenticationEmail : MonoBehaviour
{
[Header("UI References")]
public TMP_InputField emailInputField;
public TMP_InputField passwordInputField;
public GameObject signInButton;
public TextMeshProUGUI errorText;
FirebaseAuth auth;
FirebaseFirestore firestore;
void Start()
{
auth = FirebaseAuth.DefaultInstance;
firestore = FirebaseFirestore.GetInstance(FirebaseApp.DefaultInstance);
}
public void SignInWithEmail()
{
string email = emailInputField.text;
string password = passwordInputField.text;
auth.SignInWithEmailAndPasswordAsync(email, password).ContinueWith(task =>
{
if (task.IsCanceled)
{
Debug.LogError("SignInWithEmailAndPasswordAsync was canceled.");
return;
}
if (task.IsFaulted)
{
Debug.Log("Email or password incorrect");
errorText.text = "Email or password incorrect";
return;
}
FirebaseUser newUser = task.Result.User;
Debug.Log("User signed in successfully with email/password! Player ID: " + newUser.UserId);
GoToWheelScene(); // Call GoToWheelScene after successful sign-in
});
}
public void CreateAccount(string email, string password)
{
auth.CreateUserWithEmailAndPasswordAsync(email, password).ContinueWith(task =>
{
if (task.IsCanceled)
{
Debug.LogError("CreateUserWithEmailAndPasswordAsync was canceled.");
return;
}
if (task.IsFaulted)
{
Debug.LogError("CreateUserWithEmailAndPasswordAsync encountered an error: " + task.Exception);
return;
}
FirebaseUser newUser = task.Result.User;
Debug.Log("User created successfully! Player ID: " + newUser.UserId);
// Save user data to Firestore
DocumentReference userRef = firestore.Collection("players").Document(newUser.UserId);
Dictionary<string, object> userData = new Dictionary<string, object>
{
{ "email", email }
};
userRef.SetAsync(userData).ContinueWith(saveTask =>
{
if (saveTask.IsFaulted)
{
Debug.LogError("Failed to save user data to Firestore: " + saveTask.Exception);
return;
}
Debug.Log("User data saved to Firestore.");
});
GoToWheelScene();
});
}
public void GoToWheelScene()
{
Debug.Log("GoToWheelScene Executed here!");
SceneManager.LoadScene("2. Wheel");
}
}