I need help with the unity authentication sdk, specifically the username and password branch of it. The docs go over what functions to use but as a beginner they are not in depth enough. I have TMP input systems set up for the username and passwords and I have buttons to submit them but when click the submit button I get this error:
NullReferenceException: Object reference not set to an instance of an object
signin.SignUp () (at Assets/graphs/signin.cs:42)
and this is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Services.Core;
using Unity.Services.Authentication;
using System.Threading.Tasks;
using TMPro;
using UnityEngine.UI;
public class signin : MonoBehaviour
{
[SerializeField]
private TMP_InputField usernameinput;
[SerializeField]
private TMP_InputField passwordinput;
[SerializeField]
private TMP_InputField newusernameinput;
[SerializeField]
private TMP_InputField newpasswordinput;
private async void Start()
{
await UnityServices.InitializeAsync();
Debug.Log(UnityServices.State);
SetupEvents();
}
public async void login()
{
await SignInWithUsernamePasswordAsync(usernameinput.text, passwordinput.text);
}
public async void SignUp()
{
await SignUpWithUsernamePasswordAsync(newusernameinput.text, newpasswordinput.text);
}
// Setup authentication event handlers if desired
void SetupEvents() {
AuthenticationService.Instance.SignedIn += () => {
// Shows how to get a playerID
Debug.Log($"PlayerID: {AuthenticationService.Instance.PlayerId}");
// Shows how to get an access token
Debug.Log($"Access Token: {AuthenticationService.Instance.AccessToken}");
};
AuthenticationService.Instance.SignInFailed += (err) => {
Debug.LogError(err);
};
AuthenticationService.Instance.SignedOut += () => {
Debug.Log("Player signed out.");
};
AuthenticationService.Instance.Expired += () =>
{
Debug.Log("Player session could not be refreshed and expired.");
};
}
async Task SignUpWithUsernamePasswordAsync(string username, string password)
{
try
{
await AuthenticationService.Instance.SignUpWithUsernamePasswordAsync(username, password);
Debug.Log("SignUp is successful.");
}
catch (AuthenticationException ex)
{
// Compare error code to AuthenticationErrorCodes
// Notify the player with the proper error message
Debug.LogException(ex);
}
catch (RequestFailedException ex)
{
// Compare error code to CommonErrorCodes
// Notify the player with the proper error message
Debug.LogException(ex);
}
}
async Task SignInWithUsernamePasswordAsync(string username, string password)
{
try
{
await AuthenticationService.Instance.SignInWithUsernamePasswordAsync(username, password);
Debug.Log("SignIn is successful.");
}
catch (AuthenticationException ex)
{
// Compare error code to AuthenticationErrorCodes
// Notify the player with the proper error message
Debug.LogException(ex);
}
catch (RequestFailedException ex)
{
// Compare error code to CommonErrorCodes
// Notify the player with the proper error message
Debug.LogException(ex);
}
}
}
please help as I want the game done by mid august
thanks,
Mariomastr