我的分数'变量未打印到UI文本

时间:2015-09-03 06:56:49

标签: c# unity3d

[解决]

我有一个简单的拦截器游戏,我正在设计一个评分系统。我希望在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");
        }
    }   
}

1 个答案:

答案 0 :(得分:0)

感谢所有这些评论,但它现在已经解决了。这是完成的代码:

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");
     }    
 }