My 'Score' Variable Is Not Printing To UI Text
[SOLVED]
I have a simple block breaker game and I am in the process of designing a scoring system. I want to have the score shown live in the game in a UI Text Element, however my 'score' variable is not showing up. I have a menu that displays the game score, the bonus scores and the total score and all of those a working fine except for the game score (which is the same as the one shown live in the game). I have tried printing all of my variables to the console, all of which seem perfectly fine so I'm completely stumped...So could someone please look over my code and if possible tell me what I'm doing wrong and how I can fix it?
Edit: I have discovered that when I set the score variable to just bricksDestroyed it does not work so I guess that must be the problem. However I'm still not sure on how to fix it. Underneath is the Brick script attached to the bricks. In that is where bricksDestroyed is changed based of the number of bricks destroyed.
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");
}
}
}
//Brick Script
using UnityEngine;
using System.Collections;
public class Brick : MonoBehaviour {
public int maxHits;
public static int breakableCount = 0;
private int amountHit = 0;
public Sprite[] hitSprites;
private bool isBreakable;
public LevelManager levelManager;
public Score score;
void Start () {
score = GameObject.FindObjectOfType<Score>();
isBreakable = (this.tag == "Breakable");
if (isBreakable){
breakableCount++;
}
}
void OnCollisionEnter2D (Collision2D collision) {
if(isBreakable){
HandleHits();
}
}
void HandleHits () {
amountHit++;
if(amountHit >= maxHits){
//print (amountHit);
score.bricksDestroyed++;
breakableCount--;
Destroy(gameObject);
} else {
LoadSprite();
}
}
void LoadSprite () {
int spriteIndex = amountHit - 1;
this.GetComponent<SpriteRenderer>().sprite = hitSprites[spriteIndex];
}
}
It works fine here. Comment all your`if` and try this :
text.text = string.Format("Score : {0}" + "Life Bonus : {1}" +
"Total Time : {2}" + "Total Score : {3}",
score.ToString("F0")+"\n",
livesBonus.ToString("F0")+"\n",
totalTime.ToString("F0")+"\n",
totalScore.ToString("F0")+"\n");
No, still doesn't work. During the game, only the total time works, but then when the game is finished and my menu pops up with the final score, everything except the main ingame score works just fine. So I wonder if its something to do with the score variable?
it should work fine, IF,
the if statement doest what it does
the text gameobject you referenced is used in all the menus
@$$anonymous$$ewEight When you say the text gameobject (UI Element) is used in all the menus, do you mean that it has to be the same text object?
yes, because you're changing that object, so if your text object is different you would have to update the correct one
Answer by the_mr_matt · Sep 04, 2015 at 06:32 AM
Here is the final code, thanks to @MewEight for a number of suggestions.
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");
}
}