首页 > 解决方案 > 没有在 TextMeshPro 组件上更新玩家分数

问题描述

我刚刚创建了一个 TextMeshPro 文本组件,然后立即从中创建了一个 PreFab。然后,我将此预制件附加到脚本(通过将其拖动到序列化字段中),其中砖块被破坏,我正在通过 .text 属性更新文本。

当我 Debug.Log() score.text 时,它给了我正确的值。但是 UI 上的分数仍然是 0。没有出现任何错误,我已经多次交叉检查了代码。

这是代码:

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

public class BlockScript : MonoBehaviour
{
    [SerializeField] TextMeshProUGUI scoreText;
    [SerializeField] LevelScript levelScript;
    [SerializeField] AudioClip clip;
    static int gameScore;
    private void Start()
    {
        gameScore = 0;
    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        // PlayClipAtPoint creates a temporary gameObject that is created in the world space so that
        // the sound keeps on playing even though the gameobject is deleted or destroyed
        AudioSource.PlayClipAtPoint(clip, Camera.main.transform.position);
        gameScore += 83;
        scoreText.text = gameScore.ToString();
        Destroy(gameObject);        
        Debug.Log("Object name: " + collision.gameObject.name);
        Debug.Log("GameScore: " + gameScore);
        Debug.Log("Frame Count: " + Time.frameCount);       
        Debug.Log(scoreText.text);
    }
}

日志:

Object name: Ball
UnityEngine.Debug:Log(Object)
 
GameScore: 83
UnityEngine.Debug:Log(Object)
 
Frame Count: 162
UnityEngine.Debug:Log(Object)
 
Object name: Ball
UnityEngine.Debug:Log(Object)
 
GameScore: 166
UnityEngine.Debug:Log(Object)
 
Frame Count: 337
UnityEngine.Debug:Log(Object)

另外,我注意到只能将预制件拖到预制件的序列化字段上,而不是普通的游戏对象。请帮我解决这个问题,如果您需要有关此问题的任何其他信息,请告诉我。

同样在下面的图片中,你可以看到我停止玩游戏后,分数显示为747。但在游戏过程中,分数为0。

所以简而言之,在玩游戏时分数不会更新,但只有在我停止播放按钮后才能看到最终分数。

用户界面:

统一编辑器用户界面

标签: unity3d

解决方案


所以这里的问题是一个不好的参考。

该引用不是引用场景Score对象,而是指向编辑器预制件,而不是当前在场景中实例化的那个。

一种快速解决方案:

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

public class BlockScript : MonoBehaviour
{
    [SerializeField] TextMeshProUGUI scoreText;
    [SerializeField] LevelScript levelScript;
    [SerializeField] AudioClip clip;
    static int gameScore;
    private void Start()
    {
        gameScore = 0;
    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        // PlayClipAtPoint creates a temporary gameObject that is created in the world space so that
        // the sound keeps on playing even though the gameobject is deleted or destroyed
        AudioSource.PlayClipAtPoint(clip, Camera.main.transform.position);
        gameScore += 83;

        //Find the scene reference
        scoreText = GameObject.FindGameObjectWithTag("ScoreText");
        
        scoreText.text = gameScore.ToString();

        Debug.Log("Object name: " + collision.gameObject.name);
        Debug.Log("GameScore: " + gameScore);
        Debug.Log("Frame Count: " + Time.frameCount);       
        Debug.Log(scoreText.text);

        Destroy(gameObject);                
    }
}

另一种方法是避免使用该FindByTag方法(一种昂贵的方法),只需将 Score gameObject 拖放到 blocksscoreText变量即可。


推荐阅读