为什么当前文本未更新?

时间:2019-06-27 16:24:14

标签: c# unity3d

我正在尝试在游戏中创建一个生命系统,但是屏幕上的测试没有更新,尽管变量已更新。我希望我的文字也能更新。

enter image description here

LoseCollider类

[SerializeField] TextMeshProUGUI livesText;
[SerializeField] static int currentLives = 4;

public void Start()
{
    livesText.text = "Lives: " + currentLives.ToString();
    Debug.Log(currentLives);
}

private void OnTriggerEnter2D(Collider2D collision)
{
    if (currentLives > 0)
    {
        currentLives--;
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }

    if (currentLives <= 0)
    {
        SceneManager.LoadScene("Game Over");
        currentLives = 4;
    }
}

我认为您对DontDestroy是正确的问题。

来自另一个脚本

public class GameSession : MonoBehaviour
{

    // config params
    [Range(0.1f, 10f)] [SerializeField] float gameSpeed = 1f;
    [SerializeField] int pointsPerBlockDestroyed = 83;
    [SerializeField] TextMeshProUGUI scoreText;
    [SerializeField] bool isAutoPlayEnabled;


    // state variables
    [SerializeField] int currentScore = 0;


    private void Awake()
    {
        int gameStatusCount = FindObjectsOfType<GameSession>().Length;

        if (gameStatusCount > 1)
        {
            gameObject.SetActive(false);
            Destroy(gameObject);
        }
        else
        {
            DontDestroyOnLoad(gameObject);
        }
    }


    // Start is called before the first frame update
    void Start()
    {
        scoreText.text = currentScore.ToString();  

    }

    // Update is called once per frame
    void Update()
    {
        Time.timeScale = gameSpeed;
    }

    public void AddToScore()
    {
        currentScore += pointsPerBlockDestroyed;
        scoreText.text = currentScore.ToString();
    }

    public void ResetGame()
    {
        Destroy(gameObject);
    }

    public bool IsAutoPlayEnabled()
    {
        return isAutoPlayEnabled;
    }
}

我现在完全团结一致。有什么想法可以解决这个问题吗?

2 个答案:

答案 0 :(得分:2)

我怀疑您正在使用DontDestroyOnLoad

您的第一个脚本似乎与GameSession附加在同一对象上,或者位于其下面的层次结构中(一个子对象)。

这使得Start不再再被呼叫


但是,您可以向SceneManager.sceneLoaded注册回调以更新文本:

public class GameSession : MonoBehaviour
{
    // ...

    private YourFirstScript yourFirstScript;

    // rather implement a Singleton pattern this way
    private static GameSession Instance;

    private void Awake()
    {
        if(Instance)
        {
            Destroy(gameObject);
            return;
        }

        Instance = this;

        DontDestroyOnLoad(gameObject);

        // Add the callback
        // it is save to remove even if not added yet
        // this makes sure a callback is always added only once
        SceneManager.sceneLoaded -= OnSceneLoaded;
        SceneManager.sceneLoaded += OnSceneLoaded;

        yourFirstScript = GetComponentInChildren<YourFirstScript>(true);
    }

    private void OnDestroy()
    {
        // make sure to remove callbacks when not needed anymore
        SceneManager.sceneLoaded -= OnSceneLoaded;
    }

    // called everytime a scene was loaded
    private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
    {
        scoreText.text = currentScore.ToString(); 

        // Since you made it public anyway simply call the method
        // of your first script
        // You shouldn't call it Start anymore
        // because now it will be called twice
        yourFirstScript.Start();
    }

    // ...
}

注意:livesText也可以是DontDestroyOnLoad,也可以在场景加载后重新分配。否则,您仍然会得到NullReferenceException


  

如何重新分配livesText

您可以将Instance中的GameSession

public static GameSession Instance;

这有点滥用单例模式和一种有争议的..但我认为这对于快速原型设计是不错的。

,并在levelText中创建YourFirstScript

public TextMeshProUGUI levelText;

然后,您可以在相应的levelText对象上简单地添加一个新组件,例如

[RequireComponent(typeof(TextMeshProUGUI))]
public class LevelTextAssigner : MonoBehaviour
{
    // you can reference this already in the Inspector
    // then you don't need to use GetComponent
    [SerializeField] private TextMeshProUGUI _text;

    // Is called before sceneLoaded is called
    private void Awake()
    {
        if(!_text) _text = GetComponent<TextMeshProUGUI>();

        // It is possible that the first time GameSession.Instance is not set yet
        // We can ignore that because in this case the reference still exists anyway
        if(GameSession.Instance)
        {
            GameSession.Instance.YourFirstScript.leveltext = _text;
        }
    }
}

答案 1 :(得分:-1)

通过“ SetText” api设置TextMeshProUGUI。 text = "something";会更新存储值的后备字段,但不一定会触发GUI更新文本。

根据特定版本的TextMeshProUGUI,这是我以前注意到的一个问题。它不会影响所有版本的UGUI,因此对于某些用户而言,这不是问题。看起来很可能是您的情况。