首页 > 解决方案 > 在 Unity 2017.3.1f 中找不到排行榜

问题描述

因此,我正在尝试为我计划在 Android 上发布的游戏添加排行榜。我在尝试访问游戏中的排行榜时收到一条错误消息,提示“未找到排行榜”。即使我仔细检查了排行榜按钮的名称,我也会收到另一个错误,即“对象引用未设置为对象”,并且我将指定此错误出现的行。我还将指定排行榜错误发送给我的行。这是我到目前为止编写的代码:

using GooglePlayGames;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
using UnityEngine.UI;

public class LeaderboardController : MonoBehaviour {

public static LeaderboardController instance;

private const string ID = ".......";

public UnityEngine.UI.Button LeaderboardsButton;

// Use this for initialization
void Awake () {
    MakeSingleton();
    GetTheButton();
}

void GetTheButton()
{
    //this line is the problem about object reference.
    LeaderboardsButton = GameObject.Find("Leaderboards").GetComponent<UnityEngine.UI.Button>();
    LeaderboardsButton.onClick.RemoveAllListeners();
    LeaderboardsButton.onClick.AddListener(() => OpenLeaderBoardsScore());
}
// Update is called once per frame
void Start(){
    PlayGamesPlatform.Activate();
}
void OnLevelWasLoaded()
{
    PostScore();
}
void MakeSingleton()
{
    if(instance != null)
    {
        Destroy(gameObject);
    }
    else
    {
        instance = this;
        DontDestroyOnLoad(gameObject);
    }
}
public void OpenLeaderBoardsScore()
{
    if (Social.localUser.authenticated)
    {
        PlayGamesPlatform.Instance.ShowLeaderboardUI(ID);
    }
    else
    {
        Social.localUser.Authenticate((bool success) =>
        {

        });
    }
}
void PostScore()
{
    //this is the line that Leaderboard error sends me. 
    if (Social.localUser.authenticated)
    {
        Social.ReportScore(PlayerPrefs.GetInt("Score"), ID, (bool success) =>{

        });
    }
}

}

标签: c#unity3d

解决方案


推荐阅读