【问题标题】:Changing text programmatically to show score on game over screen in unity以编程方式更改文本以统一在屏幕上显示游戏分数
【发布时间】:2017-07-05 05:06:53
【问题描述】:

我一直在统一制作一个简单的 2D 游戏,它只有三个场景,开始场景、游戏场景和游戏结束场景。我想在游戏结束屏幕中显示游戏的分数。我在游戏场景中创建了一个得分管理器游戏对象,该对象使用 DontDestroyOnLoad() 函数将其转移到游戏结束屏幕中,并允许它访问由游戏管理器管理的得分。我一直在调试我的代码,分数被转换到分数管理器中,并在游戏结束屏幕加载时被维护,但由于某种原因,它不会让我更新分数文本对象。这是我的代码:

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

public class ScoreManager : MonoBehaviour {

    public static ScoreManager Instance;

    private GameController gameController;

    private int scoreInstance;

    private Text scoreText;

    // When scene is first loaded
    void Awake() {

        this.InstantiateController();

    }

    // Use this for initialization
    void Start () {

        GameObject gameControllerObject = GameObject.FindWithTag("GameController");

        if (gameControllerObject != null)
        {
            gameController = gameControllerObject.GetComponent<GameController>();
        }

        GameObject scoreTextObject = GameObject.FindWithTag("ScoreText");

        if (scoreTextObject != null)
        {
            scoreText = scoreTextObject.GetComponent<Text>();
        }

        scoreInstance = 0;

        scoreText.text = "";

    }

    // Update is called once per frame
    void Update () {

        scoreInstance = gameController.score;
        Debug.Log("Score: " + scoreInstance.ToString());

        scoreText.text = scoreInstance.ToString();

    }

    private void InstantiateController ()
    {
        if (Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(this);
        }
        else if (this != Instance)
        {
            Destroy(this.gameObject);
        }
    }
}

所以我尝试在启动函数中以编程方式收集“乐谱文本” ui 组件,因为我认为我不能将其公开并拖入文本组件,因为乐谱管理器实际上与乐谱处于不同的场景中文本对象。我还尝试添加这一整段代码以将文本组件收集到更新函数中,以便当分数管理器实际上是屏幕游戏的一部分时,它可以做到这一点。似乎没有任何效果,我不知道为什么。有人可以帮我吗?此外,我不断收到“NullReferenceException:对象引用未设置为对象的实例”错误。提前感谢您的帮助。

【问题讨论】:

  • start 方法的最后一行和 Update 中的所有行都可能给出空异常,因为您不知道对象是否已初始化,但您可以访问它们的属性。首先调试你的“GetComponents”,看看是否有任何东西被使用
  • 谢谢! :) 我调试了两个 GetComponents 并且错误确实是由使分数无法更新的相同问题引起的,使用@JasonLang 建议的方法也修复了我的 NullReference 错误。

标签: c# unity3d


【解决方案1】:

Unity Start 函数仅在脚本第一次启用时调用,即不是每次 DontDestroyOnLoad 对象的场景发生变化时。

因此,如果您需要在场景更改后连接一些更改,您需要检测场景更改,或者让从该场景开始的对象触发您要运行的代码。

在新场景中使用另一个对象来触发事情很简单而且非常简单,但是您可以将一个内置函数添加到其他对象中:

void OnLevelWasLoaded(int currentLevel)
{
}

这将在关卡更改时调用,并为您提供关卡编号(遗憾的是没有名字)。但是,以上内容已弃用,他们希望您使用 Unity 的 SceneManager,因此现在设置它的正确方法是:

Unity 5 OnLevelWasLoaded?

Start()
{
    SceneManager.sceneLoaded += this.OnLoadCallback;
}

void OnLoadCallback(Scene scene, LoadSceneMode sceneMode)
{
    // you can query the name of the loaded scene here
}

【讨论】:

  • 太棒了,这解决了我的问题。我最终使用 Unity 的 SceneManager 来检查活动场景,当它等于游戏结束场景时,我运行了 GetComponent 代码。谢谢您的帮助! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-27
相关资源
最近更新 更多