我的';得分';变量未打印到UI文本
本文关键字:UI 文本 打印 变量 得分 我的 | 更新日期: 2023-09-27 18:11:01
[已解决]
我有一个简单的盖帽游戏,我正在设计一个得分系统。我想在UI文本元素中实时显示游戏中的分数,但我的"分数"变量没有显示。我有一个菜单,显示游戏分数、奖金分数和总分,以及除游戏分数(与游戏中实时显示的分数相同(外的所有工作罚款。有人能检查一下我的代码吗?如果可能的话,告诉我我做错了什么,以及如何修复它?
*编辑:很抱歉忘了提一下,当所有变量都打印到控制台时,它们都能完美工作。
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Score : MonoBehaviour {
public Text text;
public float totalScore = 0;
public float bonusScore = 0;
public float score = 0;
public float bricksDestroyed;
public int livesRemaining = 5;
public float totalTime;
public int livesBonus;
public bool isLivesBonus;
public bool isTimeBonus;
public bool isTotal;
public bool isScore;
void Update () {
if(!Paddle.isPaused){
totalTime += (Time.deltaTime / Time.timeScale) / 5;
}
score = (bricksDestroyed * (5 * Time.timeScale));
bonusScore = (50 * livesRemaining) - totalTime;
totalScore = score + bonusScore;
print (score);
livesBonus = livesRemaining * 50;
if(isScore){
text.text = score.ToString("F0");
}else if(isLivesBonus){
text.text = livesBonus.ToString("F0");
}else if(isTimeBonus){
text.text = totalTime.ToString("F0");
}else if(isTotal){
text.text = totalScore.ToString("F0");
}
}
}
感谢所有这些评论,但它现在已经解决了。这是完整的代码:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Score : MonoBehaviour {
public Text inGameScore;
public Text loseScore;
public Text winScore;
public float totalScore = 0;
public float bonusScore = 0;
public float score = 0;
public float bricksDestroyed;
public int livesRemaining = 5;
public float totalTime;
public int livesBonus;
public bool isLivesBonus;
public bool isTimeBonus;
public bool isTotal;
public bool isScore;
void Update () {
if(!Paddle.isPaused){
totalTime += (Time.deltaTime / Time.timeScale) / 5;
}
score = (bricksDestroyed * (5 * Time.timeScale));
bonusScore = (50 * livesRemaining) - totalTime;
totalScore = score + bonusScore;
//print (score);
livesBonus = livesRemaining * 50;
inGameScore.text = string.Format("{0}",
score.ToString("F0")+"'n");
loseScore.text = string.Format("{0}" + "{1}" +
"{2}" + "{3}",
score.ToString("F0")+"'n",
totalTime.ToString("F0")+"'n",
livesBonus.ToString("F0")+"'n",
totalScore.ToString("F0")+"'n");
winScore.text = string.Format("{0}" + "{1}" +
"{2}" + "{3}",
score.ToString("F0")+"'n",
totalTime.ToString("F0")+"'n",
livesBonus.ToString("F0")+"'n",
totalScore.ToString("F0")+"'n");
}
}