question

shionyorn0202 avatar image
shionyorn0202 asked

Player Statistic and different SetActive,Player Statistics and different SetActive

how to get Player Statistics and implement different SetAvtive according to PlayerLevel?

i implemented "PlayerLevel" in my PlayFab Statistics.

but i dont know how to use it.

I would like to do like this:

if PlayerLevel < 5, GameObject.SetActive(true);

,

How to get Player Status and implement different SetAvtive according to PlayerLevel?

i would like to do something like this:

if playerLevel < 5, Gameobect.SetActive(true);

i implemented "playerLevel" in the Playfab Statistics.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Sarah Zhang avatar image
Sarah Zhang answered

According to your description, it seems you are using the Unity Game Engine to develop your project. Before you try to get the Player’s Statistics in the Unity C# Script, please confirm you have download our Unity SDK from this page. And please confirm you have followed this QuickStart to make your first API call. If you have met the above requirements, you can use the Client API

GetPlayerStatistics to retrieve the Player’s Statistics in the Client.

The sample code would be something like this.

using PlayFab;
using PlayFab.ClientModels;
using System.Collections.Generic;
using UnityEngine;


public class BasicTestingScript : MonoBehaviour
{
    public void Start()
    {
        if (string.IsNullOrEmpty(PlayFabSettings.staticSettings.TitleId))
        {
            /*
            Please change the titleId below to your own titleId from PlayFab Game Manager.
            If you have already set the value in the Editor Extensions, this can be skipped.
            */
            PlayFabSettings.staticSettings.TitleId = "[YourTitleId]";
        }
        var request = new LoginWithCustomIDRequest { CustomId = "GettingStartedGuide", CreateAccount = true };
        PlayFabClientAPI.LoginWithCustomID(request, OnLoginSuccess, OnFailure);
    }


    private void GetPlayerLevel()
    {
        var request = new GetPlayerStatisticsRequest { StatisticNames = new List<string> { "PlayerLevel" } };
        PlayFabClientAPI.GetPlayerStatistics(request, OnGetPlayerLevelSuccess, OnFailure);
    }


    private void OnGetPlayerLevelSuccess(GetPlayerStatisticsResult result)
    {
        int playerLevel = 0;
        foreach (var item in result.Statistics)
        {
            if (item.StatisticName == "PlayerLevel")
            {
                playerLevel = item.Value;
            }
        }
        if (playerLevel > 5)
        {
            //Do GameObject.SetActive(true) here
            Debug.Log("Enable the GO");
        }
    }


    private void OnLoginSuccess(LoginResult result)
    {
        Debug.Log("Congratulations, you made your first successful API call!");
        GetPlayerLevel();
    }


    private void OnFailure(PlayFabError error)
    {
        Debug.LogWarning("Something went wrong. ");
        Debug.LogError("Here's some debug information:");
        Debug.LogError(error.GenerateErrorReport());
    }
}
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.