问题描述
我有一个非常简单的脚本。我在TMP_Text文件上使用[Serializefield]。在检查器中,我已将文本文件从“层次结构”拖动到UI控制器的检查器中的序列化字段中。当我单击“播放”时,检查器将参考放到tmp上并说那里没有任何内容。我有能力在文件运行后重新整理它,它将正常工作。
为什么我在播放时丢失文件?
[Serializefield] TMP_Text score;
int points;
// Start is called before the first frame update
void Awake()
{
points = 0;
score = GetComponent<TMP_Text>();
}
// Update is called once per frame
void Update()
{
score.SetText(points.ToString());
}
public void AddToscore(int score)
{
points += score;
}
这是一个非常简单的脚本。我要附加的文件没有脚本或任何其他文件。它只是一个基本的文本网格物体对象。
解决方法
清醒时,您重新分配分数。问题是您要做一个仅搜索当前GameObject的GetComponent。由于UIController不包含TMP_Text组件,因此它返回null,因此您将丢失引用。如果只删除该行(分数= GetComponent