unity3d - 没有在 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。
所以简而言之,在玩游戏时分数不会更新,但只有在我停止播放按钮后才能看到最终分数。
用户界面:
解决方案
所以这里的问题是一个不好的参考。
该引用不是引用场景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
变量即可。
推荐阅读
- kubernetes - Kubernetes:找不到指向的最终端点。验证服务时出错:获取服务时出错:找不到服务
- java - Powermock:如何在枚举常量中模拟方法
- ios - 动态字体类型的辅助功能检查器警告
- python - 连接两个具有不同索引和不同形状的元组
- python - 如果在 Scrapy 中遇到某些情况,如何摆脱爬网
- java - 调用 init 方法失败;嵌套异常是 java.lang.IllegalArgumentException:不是托管类型:
- postgresql - Postgres中的加权中位数计算
- javascript - 如何在反应中发送到其他组件onChange选择
- xamarin.forms - 如何使用 Xamarin.ios 在 IOS 中运行后台任务
- powershell - 搜索注册表并创建新项目