【问题标题】:error CS0103: The name 'playfabManager' does not exist in the current context错误 CS0103:当前上下文中不存在名称“playfabManager”
【发布时间】:2021-04-04 11:47:48
【问题描述】:

我刚开始使用 Unity,制作了一款名为 Flappy Duck 的小游戏。 我想要一个在线排行榜,这样每个人都可以击败彼此的高分。 我偶然发现了这个错误 Assets\scripts\PlayerController.cs(65,9): error CS0103: The name 'playfabManager' does not exist in the current context

我找不到任何问题。顺便说一句,我正在使用教程 来自 CoCo Code https://www.youtube.com/watch?v=e2RXDso6fWU&t=266s 他使用 playfab 我正在尝试 让它在我自己的游戏中工作。 这是使文本显示高分的脚本

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

public class HighSScore : MonoBehaviour
{
    public Text HighScore;
    // Start is called before the first frame update
    void Start()
    {
        HighScore.text = PlayerPrefs.GetInt("highscore").ToString();
    }
}

这是 PLAYFAB 经理

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

public class PlayFabManager : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Login();   
    }
    void Login()
    {
        var request = new LoginWithCustomIDRequest
        {
            CustomId = SystemInfo.deviceUniqueIdentifier,
            CreateAccount = true
        };
        PlayFabClientAPI.LoginWithCustomID(request, OnSuccess, OnError);
    }
    
    void OnSuccess(LoginResult result)
    {
    Debug.Log("Successful login/account create!");
    }

    void OnError(PlayFabError error)
    {
    Debug.Log("Error while logging in/creating account!");
    Debug.Log(error.GenerateErrorReport());
    }
    public void SendLeaderboard(int score)  
    {
        var request = new UpdatePlayerStatisticsRequest
        {
            Statistics = new List<StatisticUpdate>
            {
                new StatisticUpdate
                {
                    StatisticName = "score",
                    Value = score
                }
            }
        };
        PlayFabClientAPI.UpdatePlayerStatistics(request, OnLeaderboardUpdate, OnError);
    }

    void OnLeaderboardUpdate(UpdatePlayerStatisticsResult result)
    {
        Debug.Log("succsessfull send leaderboard");
    }
}`

最后是播放器控制器 错误是否基于第 65 行 我正在尝试将高分发送到 playfab 中的排行榜。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class PlayerController : MonoBehaviour
{
    [SerializeField] TextMeshProUGUI scoreText;
    Rigidbody2D bird;
    int score = 0;
    bool dead = false;
    int highscore = 0;


    // Start is called before the first frame update
    void Start()
    {
        bird = transform.GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    public void update ()
    {
        if (Input.GetKeyDown("space") && !dead)
        {
            bird.velocity = new Vector2(0, 6f);
        }
        if (Input.GetKeyDown("r"))
        {
            SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().buildIndex);
        }
        if (score > highscore)
        {
            highscore = score;
            SendLeaderboard();
            PlayerPrefs.SetInt("highscore", score);
        }
    }
    void OnCollisionEnter2D()
    {
        dead = true;
        score = 0;
        scoreText.text = "0";
    }
    void OnTriggerExit2D(Collider2D col)
    {
        if (col.gameObject.tag == "PointTrigger")
        {
            score++;
            scoreText.text = score.ToString();
        }
    }
    void OnTriggerExit(Collider col)
    {
        if (col.gameObject.tag == "PointTrigger")
        {
            score++;
            scoreText.text = score.ToString();
        }
    }
    public void SendLeaderboard()
    {
        playfabManager.SendLeaderboard(highscore);
    }
}

我希望有人可以帮助我。

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    很可能在PlayerController 你想要一个

    [SerializeField] private PlayFabManager playfabManager;
    

    并通过 Inspector 或在运行时通过例如引用它

    private void Awake ()
    {
        // Try and get the component if it is attached to the same object as this
        if(! playfabManager) playfabManager = GetComponent<PlayFabManager>();
    
        // Or try and find it anywhere in the scene
        if(! playfabManager) playfabManager = FindObjectOfType<PlayFabManager>();
    
        // Or simply create and attach it to the same object as this one
        if(! playfabManager) playfabManager = gameObject.AddComponent<PlayFabManager>();
    } 
    

    或者 - 在我看来,这将是更正确的解决方案 - PlayerFabManager 应该完全是 MonoBehaviour,而是简单地做

    public class PlayFabManager
    {
        public void Login()
        {
            ...  
        }
    
        ...
    }
    

    而在PlayerController 你宁愿这样做

    private readonly PlayFabManager playfabManager = new PlayFabManager();
    
    private void Start ()
    {
        playfabManager.Login();
    }
    

    【讨论】:

      猜你喜欢
      • 2019-01-05
      • 2021-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多