C# 使用脚本更改画布中的TextMeshPro UI文本时出现问题
我在画布中使用TextMeshPro从画布中的脚本更改评分值时遇到问题。我得到了一个NullPointerException错误,因为我觉得我没有正确地引用score文本文件,但我不知道如何正确地进行引用。 错误:C# 使用脚本更改画布中的TextMeshPro UI文本时出现问题,c#,unity3d,C#,Unity3d,我在画布中使用TextMeshPro从画布中的脚本更改评分值时遇到问题。我得到了一个NullPointerException错误,因为我觉得我没有正确地引用score文本文件,但我不知道如何正确地进行引用。 错误: public class Score : MonoBehaviour { static int scoreValue1 = 0; static int scoreValue2 = 0; private TMP_Text score1; private TMP_Text score2;
public class Score : MonoBehaviour
{
static int scoreValue1 = 0;
static int scoreValue2 = 0;
private TMP_Text score1;
private TMP_Text score2;
public void PlayerOneScored() {
Debug.Log("Player One Scored");
scoreValue1++;
TMP_Text score1 = GetComponent<TextMeshProUGUI>();
Debug.Log("P1 Score: " + scoreValue1);
score1.text = "Score: " + scoreValue1;
}
public void PlayerTwoScored()
{
Debug.Log("Player Two Scored");
scoreValue2++;
TMP_Text score2 = GetComponent<TMP_Text>();
Debug.Log("P2 Score: " + scoreValue2);
score2.text = "Score: " + scoreValue2;
}
}
NullReferenceException:对象引用未设置为对象的实例
Score.playerScored()(位于Assets/Scripts/Score.cs:20)
代码:
public class Score : MonoBehaviour
{
static int scoreValue1 = 0;
static int scoreValue2 = 0;
private TMP_Text score1;
private TMP_Text score2;
public void PlayerOneScored() {
Debug.Log("Player One Scored");
scoreValue1++;
TMP_Text score1 = GetComponent<TextMeshProUGUI>();
Debug.Log("P1 Score: " + scoreValue1);
score1.text = "Score: " + scoreValue1;
}
public void PlayerTwoScored()
{
Debug.Log("Player Two Scored");
scoreValue2++;
TMP_Text score2 = GetComponent<TMP_Text>();
Debug.Log("P2 Score: " + scoreValue2);
score2.text = "Score: " + scoreValue2;
}
}
层次结构图:
更新立即让它工作!最后这是我的课
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class Score : MonoBehaviour
{
static int scoreValue1 = 0;
static int scoreValue2 = 0;
public TextMeshProUGUI score1;
public TextMeshProUGUI score2;
void Awake()
{
score1.text = "Score: " + scoreValue1;
score2.text = "Score: " + scoreValue2;
}
public void PlayerOneScored() {
if (scoreValue1 < 10) {
scoreValue1++;
Debug.Log("Player One Scored");
Debug.Log("P1 Score: " + scoreValue1);
score1.text = "Score: " + scoreValue1;
}
else { }
}
public void PlayerTwoScored()
{
if (scoreValue2 < 10)
{
scoreValue2++;
Debug.Log("Player Two Scored");
Debug.Log("P2 Score: " + scoreValue2);
score2.text = "Score: " + scoreValue2;
}
else { }
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
使用TMPro;
公共课成绩:单一行为
{
静态int scoreValue1=0;
静态int scoreValue2=0;
公共文本Prougui评分1;
公共文本Prougui评分2分;
无效唤醒()
{
score1.text=“分数:”+scoreValue1;
score2.text=“Score:+scoreValue2;
}
公众假期{
如果(分数值1<10){
scoreValue1++;
Debug.Log(“玩家一分”);
Debug.Log(“P1分数:+scoreValue1”);
score1.text=“分数:”+scoreValue1;
}
else{}
}
公共无效播放机()
{
如果(分数值2<10)
{
scoreValue2++;
Debug.Log(“玩家二分”);
Log(“P2分数:+scoreValue2”);
score2.text=“Score:+scoreValue2;
}
else{}
}
}
在您的评论中,您说您的脚本已附加到画布
游戏对象
但是您正在使用
GetComponent<TextMeshProUGui>();
另一种选择是在运行时获取相应的子对象(当然这更容易出错)
score1=transform.GetChild(0.GetComponemt();
...
score2=transform.GetChild(1)。GetComponent();
我怀疑事实上。。我将使用Draco18s:score1的null
,因为GetComponent
失败。。你确定这个脚本在正确的游戏对象上,同样的游戏对象也有TextMeshProUGui
组件吗?该方法中没有其他任何东西可能引发此异常。。通过调试断点可以很容易地看到它,所以我认为它是重复的。在另一个示例中,您还使用了TMP\u Text
。。也许你也应该在第一个线程中使用它?如果你认为这不是重复的,请说明你从重复线程中到底尝试了什么/没有理解什么。@derHugo Score脚本附加到ScoreCanvas,两个文本文件都附加在那里。我阅读了链接和许多其他页面,但我不明白我做错了什么,我是unity的初学者,其他科目的工作量很大,所以我想问你,希望你不要因为我不明白我做错了什么而再次关闭我的帖子。复制的线程是非常非常广泛,我找不到它可以帮助我。我基本上是个哑巴。这就是我需要帮助的原因。非常感谢!所以我把它改为public并分配了它(我以前尝试过这个,但现在我看到我用GetComponent覆盖了它,所以我删除了它)。我现在唯一的问题是,对于我的游戏,每次玩家得分时场景都会重置,因此会重置文本(但不是分数的值)。所以我看到分数改变了,然后它立即变回迪富尔特值。我该如何在重置场景时保持写入的字符串?没关系,让它正常工作。将更新我所做的事情。非常感谢。
// Now both these fields will appear in the Inspector
// so you can simply drag&drop the according GameObjects into the slots
[SerializeField] private TextMeshProUGUI score1;
public TextMeshProUGUI score2;
score1 = transform.GetChild(0).GetComponemt<TextMeshProUGUI>();
...
score2 = transform.GetChild(1). GetComponent<TextMeshProUGUI>();