C# Unity UI文本在计时器中带有gameobject和Nullreference的UnderMap
我正在尝试在unity中制作一个2D破砖游戏。问题如下:C# Unity UI文本在计时器中带有gameobject和Nullreference的UnderMap,c#,unity5,unity3d-2dtools,C#,Unity5,Unity3d 2dtools,我正在尝试在unity中制作一个2D破砖游戏。问题如下: NullReferenceException: Object reference not set to an instance of an object UiManager.Update () (at Assets/UiManager.cs:21) 1) 文本上的游戏和按钮被砖块重叠,如图所示: 2) 单击菜单按钮后,我得到了NullReferenceException,如下所示: NullReferenceException: Ob
NullReferenceException: Object reference not set to an instance of an object
UiManager.Update () (at Assets/UiManager.cs:21)
1) 文本上的游戏和按钮
被砖块重叠,如图所示:
2) 单击菜单
按钮后,我得到了NullReferenceException
,如下所示:
NullReferenceException: Object reference not set to an instance of an object
UiManager.Update () (at Assets/UiManager.cs:21)
UiManager脚本如下所示:
public class UiManager : MonoBehaviour {
float score=0;
public Text scoreText;
public Text endText;
public Button[] buttons;
public Text TIMER;
float time=30f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
time -=Time.deltaTime;
TIMER.text = "TIME: " + (int)time;
if (time < 1) {
GameOverF ();
Time.timeScale = 0;
}
}
public void IncrementScore(){
score++;
scoreText.text = "SCORE: " + score;
}
public void IncrementBadScore(){
score-=11;
scoreText.text = "SCORE: " + score;
}
public void IncrementGoodScore(){
score+=11;
scoreText.text = "SCORE: " + score;
}
public void IncrementRedScore(){
score-=2;
scoreText.text = "SCORE: " + score;
}
public void GameOverF(){
endText.gameObject.SetActive (true);
foreach (Button button in buttons) {
button.gameObject.SetActive (true);
}
}
public void exit(){
Application.Quit ();
}
public void Play(){
Application.LoadLevel ("level1");
}
public void Menu(){
Application.LoadLevel ("menu");
}
}
公共类UiManager:MonoBehavior{
浮动分数=0;
公共文本;
公共文本结束文本;
公共按钮[]按钮;
公共文本定时器;
浮动时间=30f;
//用于初始化
无效开始(){
}
//每帧调用一次更新
无效更新(){
time-=time.deltaTime;
TIMER.text=“时间:”+(int)时间;
如果(时间<1){
GameOverF();
Time.timeScale=0;
}
}
public void IncrementScore(){
分数++;
scoreText.text=“分数:”+分数;
}
public void IncrementBadScore(){
得分-=11分;
scoreText.text=“分数:”+分数;
}
公共无效增量goodscore(){
分数+=11分;
scoreText.text=“分数:”+分数;
}
public void IncrementRedScore(){
得分-=2分;
scoreText.text=“分数:”+分数;
}
公共部门{
endText.gameObject.SetActive(true);
foreach(按钮中的按钮){
button.gameObject.SetActive(true);
}
}
公共无效出口(){
Application.Quit();
}
公共游戏{
Application.LoadLevel(“level1”);
}
公共无效菜单(){
Application.LoadLevel(“菜单”);
}
}
此错误导致我的游戏在下次单击Play后挂起,因此我只需重新启动游戏
代码行如下所示:
public class UiManager : MonoBehaviour {
float score=0;
public Text scoreText;
public Text endText;
public Button[] buttons;
public Text TIMER;
float time=30f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
time -=Time.deltaTime;
TIMER.text = "TIME: " + (int)time;
if (time < 1) {
GameOverF ();
Time.timeScale = 0;
}
}
public void IncrementScore(){
score++;
scoreText.text = "SCORE: " + score;
}
public void IncrementBadScore(){
score-=11;
scoreText.text = "SCORE: " + score;
}
public void IncrementGoodScore(){
score+=11;
scoreText.text = "SCORE: " + score;
}
public void IncrementRedScore(){
score-=2;
scoreText.text = "SCORE: " + score;
}
public void GameOverF(){
endText.gameObject.SetActive (true);
foreach (Button button in buttons) {
button.gameObject.SetActive (true);
}
}
public void exit(){
Application.Quit ();
}
public void Play(){
Application.LoadLevel ("level1");
}
public void Menu(){
Application.LoadLevel ("menu");
}
}
现在如何删除这些错误?我想不出来,请帮忙 好的,我猜你没有在inspector中设置
文本
?我已经在inspector中设置了文本,所以我猜你没有在inspector中设置文本
?我已经在inspector中设置了文本