Sign In Register

How can we help you today?

Start a new topic
Answered

Struggling with authentication

I'm new to Gamesparks but i already like it, good job.
Btw I've a problem that I can't figure out how to solve.
Basically I'm trying to authenticate my user, so I've this `EmptyGameObject` to which i linked the GameSparksManager.cs that inside has this code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameSparksManager : MonoBehaviour {
	private static GameSparksManager instance = null;

	void Awake() 
	{
		if (instance == null) {
			instance = this;
			DontDestroyOnLoad (this.gameObject);

		} else {
			Destroy (this.gameObject);
		}
	}
}

 

 Then i made a form where i'm trying to login my user and on submit it runs this code:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using GameSparks.Core;

public class AuthenticatePlayer : MonoBehaviour {
	public Text 
	userNameInput,	
	passwordInput;

	public void AuthenticatePlayerButton() 
	{
		Debug.Log (GS.Available);
		Debug.Log ("Authenticating User");
		new GameSparks.Api.Requests.AuthenticationRequest()
			.SetUserName(userNameInput.text)
			.SetPassword(passwordInput.text)
			.Send((response) => {
				if(response.HasErrors) 
				{
					Debug.Log("Error while authenticating player \nuser: " + response.DisplayName);
				}
				else 
				{
					Debug.Log("User authenticated \n" + response.Errors.JSON.ToString());	
				}			
			});
	}
}

 As you can see i'm checking if i'm connected to GS and it returns `true` but i get this error:


image


Anyone have an idea on what i'm doing wrong??


Login and Registration are on the same scene but registration works and login not.



Best Answer
Hey there!

The null reference you are getting is being caused by your response logic.

if(response.HasErrors)//If you have an Error 
				{
                                        //Do this.....
					Debug.Log("Error while authenticating player \nuser: " + response.DisplayName);
				}
				else //if you dont have an error
				{

                                        //Do this.....
					Debug.Log("User authenticated \n" + response.Errors.JSON.ToString());	
				}			

In your else ( no errors) you are looking for errors that do not exist(Null reference). You can correct this by using !response.HasErrors.

Regards,
-Dave

1 Comment

Answer
Hey there!

The null reference you are getting is being caused by your response logic.

if(response.HasErrors)//If you have an Error 
				{
                                        //Do this.....
					Debug.Log("Error while authenticating player \nuser: " + response.DisplayName);
				}
				else //if you dont have an error
				{

                                        //Do this.....
					Debug.Log("User authenticated \n" + response.Errors.JSON.ToString());	
				}			

In your else ( no errors) you are looking for errors that do not exist(Null reference). You can correct this by using !response.HasErrors.

Regards,
-Dave

Login to post a comment