Sign In Register

How can we help you today?

Start a new topic

How to use the DeviceAuthorizatonRequest in Unity

 Hi,


its a bit unclear on how to use the DeviceAuthorizationRequest.


I'm using the following Coding in Start()


 

new DeviceAuthenticationRequest()
        .Send((response) =>
        {
            string authToken = response.AuthToken;
            string displayName = response.DisplayName;
            bool? newPlayer = response.NewPlayer;
            GSData scriptData = response.ScriptData;
            var switchSummary = response.SwitchSummary;
            string userId = response.UserId;
        });

 However, I'm getting the following error message, marking the line "new DeviceAuthenticationRequest()"


NullReferenceException: Object reference not set to an instance of an object
GameSparks.Api.Requests.DeviceAuthenticationRequest..ctor ()

 What am I doing wrong?


I just found the solution.

Seems that I can't put the DeviceAuthenticationRequest in Start().

I invoked the authenticate Method 2 seconds and authentication works now.

This is not very nice but I can workaround this. But it does seem like a bug to me

 

Hi Alex,


I'd recommend having a look at our Unity Getting started Guide. Particularly the Authentication example. It should cover your needs here. If you have any further questions after that just let us know.


Regards,

Liam

Hi, thanks for your response.

Maybe I'm missing the point of the DeviceAuthenticationRequest, but I want to use GS in a Unity WebApp. My understnading is that I can use it there.
I did setup everything according to doc you linked. I have set the Gamesparks settings file and created my own Gamesparksmanager


 

public class FD_Gamesparksmanager : MonoBehaviour
{
    private static FD_Gamesparksmanager instance = null;
    void Awake()
    {
        if (instance == null) // check to see if the instance has a reference
        {
            instance = this; // if not, give it a reference to this class...
            DontDestroyOnLoad(this.gameObject); // and make this object persistent as we load new scenes
        }
        else // if we already have a reference then remove the extra manager from the scene
        {
            Destroy(this.gameObject);
        }
    }

    // Use this for initialization
    void Start()
    {
        authenticate();
    }

    void authenticate()
    {
        new GameSparks.Api.Requests.DeviceAuthenticationRequest()
            .SetDisplayName("Randy2")
            .Send((response) =>
            {
                if (!response.HasErrors)
                {
                    Debug.Log("Device Authenticated...");//

                }
                else
                {
                    Debug.Log("Error Authenticating Device...");
                }
            });
        }

}

 

public class FD_Gamesparksmanager : MonoBehaviour
{
    private static FD_Gamesparksmanager instance = null;
    void Awake()
    {
        if (instance == null) // check to see if the instance has a reference
        {
            instance = this; // if not, give it a reference to this class...
            DontDestroyOnLoad(this.gameObject); // and make this object persistent as we load new scenes
        }
        else // if we already have a reference then remove the extra manager from the scene
        {
            Destroy(this.gameObject);
        }
    }

    // Use this for initialization
    void Start()
    {
        authenticate();
    }

    void authenticate()
    {
        new GameSparks.Api.Requests.DeviceAuthenticationRequest()
            .SetDisplayName("Randy2")
            .Send((response) =>
            {
                if (!response.HasErrors)
                {
                    Debug.Log("Device Authenticated...");//

                }
                else
                {
                    Debug.Log("Error Authenticating Device...");
                }
            });
        }

}

 

Still getting this error:


NullReferenceException: Object reference not set to an instance of an object
GameSparks.Api.Requests.DeviceAuthenticationRequest..ctor ()
FD_Gamesparksmanager.authenticate () (at Assets/FD_Gamesparksmanager.cs:44)
FD_Gamesparksmanager.Start () (at Assets/FD_Gamesparksmanager.cs:28)

When using the registrationrequest from the example, everything works fine!

 

Login to post a comment