Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by the_mr_matt · Sep 03, 2015 at 09:21 AM · c#uitextunity 4.6

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];    
     }
 }

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Positive7 · Sep 03, 2015 at 10:30 AM 1
Share

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

avatar image the_mr_matt · Sep 03, 2015 at 06:01 PM 0
Share

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?

avatar image MewEight · Sep 04, 2015 at 03:34 AM 0
Share

it should work fine, IF,

  • the if statement doest what it does

  • the text gameobject you referenced is used in all the menus

avatar image the_mr_matt · Sep 04, 2015 at 04:48 AM 0
Share

@$$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?

avatar image MewEight · Sep 04, 2015 at 06:08 AM 0
Share

yes, because you're changing that object, so if your text object is different you would have to update the correct one

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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");
     }    
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to do Ammo UI in Unity 5.3.4?,How to do Ammo UI in Unity 5 1 Answer

Text Objects do Lerp 0 Answers

ui.text string matching doesn't work 0 Answers

[HELP] To show wave number on a zombie survival game. 0 Answers

Best way to attatch text to text script c# 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges