Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
2
Question by wyph · Mar 18, 2012 at 09:31 AM · guiscoregui.label

Updating GUI.Label for Score

I am trying to use a GUI.Label to update the score of my game. My problem is I do not know how to get the score to increase on the GUI. I know that label is just used to display information, and I have tried using a string instead, but it didn't work for me. I might have done it wrong so I am asking for help. Here is my script for the text

 public var score : int = 0;

function OnGUI () { GUI.Label (Rect (25, 25, 100, 30), "Score:" + score); }

function OnCollisionEnter(collision : Collision) {
//if I collide with a bullet,destroy myself

 if(collision.gameObject.tag=="Bullet")
     {    
     score += 1;
     Destroy(gameObject);
     }
 }

EDIT: The GUI shows up and shows a starting score of 0 when I destroy the object in question, the score does not change in the GUI but if I Debog.Log, the score goes up by 1.

Comment
Add comment · Show 1
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 McDardy · Aug 25, 2012 at 01:52 PM 0
Share

I have same problem... any chances that somebody knows something and is willing to share? I can always post another question, but that's not neccesary, I think. In one script (connected with player) I've hit points, exp, etc. and after colliding with box it ads exps taking hps, etc... Problem is that my GUI (shows after key "$$anonymous$$" or "I") is not updating. For me everything is like above besides fact that I'm taking "score" from another script so is going like:

GUI.Label (r_expLabel, currentEXP.ToString(), "Text Amount");

where var currentEXP = playerInfo.actualEXP;

I was trying reimporting and everything.

5 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by _samuel12345 · Mar 29, 2017 at 03:52 PM

I was having the same problem. Don't make the variable "score" a public variable, make it static. So instead of:

public var score : int = 0;

Use:

 static var score : int = 0;

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
avatar image
-1

Answer by Kleptomaniac · Mar 18, 2012 at 10:30 AM

Instead of:

 function OnGUI () { 
 GUI.Label (Rect (25, 25, 100, 30), "Score:" + score); 
 }

Try:

 function OnGUI () { 
 GUI.Label (Rect (25, 25, 100, 30), "Score:" + score.ToString()); 
 }

Converting the score int to a string value so it can be displayed in the GUI.Label.

Hope that helps, Klep

Comment
Add comment · Show 3 · 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
avatar image wyph · Mar 19, 2012 at 03:22 AM 0
Share

Well, I tried to change the score to score.toString() yet it still is not updating itself to the score's new amount. I tried using a debug.log to see if the score is changing, it is changing but the number by the text is not.

avatar image Kleptomaniac · Mar 19, 2012 at 10:25 AM 0
Share

Could you try just commenting the Destroy(gameObject) line and running the script like that? I bet you it has something to do with the fact that OnGUI does not have a chance to update before the script is destroyed. That is just speculation of course though ... :P

avatar image McDardy · Aug 25, 2012 at 08:57 PM 0
Share

I tried that... nothings change beside the fact that object (in my case is tester $$anonymous$$r.Cube) is not destroying - best of it that script aplied to player still is counting everything right... only that GUI label. Please help...

avatar image
-1

Answer by tianjishu · Mar 19, 2012 at 03:42 AM

pragma strict

ar score : int = 0; function OnGUI () { GUI.Label (Rect (25, 25, 100, 30), "Score:" + score); }

I copy your function and running.Your function have not problem!!!!

Comment
Add comment · Show 1 · 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
avatar image wyph · Mar 19, 2012 at 03:47 AM 0
Share

Its not that my code has problems. $$anonymous$$y score does not update upon destroying the object in question. When I debug.log, the console shows the score going up, but the GUI never changes. its stays saying Score: 0

avatar image
0

Answer by gameangel · Feb 03, 2014 at 06:50 PM

My Label does update the score "in the console it says so" but it dosent show up in the game. Help?

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
avatar image
0

Answer by morgan23 · Feb 03, 2014 at 09:25 PM

hmm from what I seen your code should work I have an example I did too test my health in game it's in c# through but should be easy too change. void Update () { if(Input.GetKeyDown(GUIKey)) { Life -=1; } } it update's correct when is pressed my GUI label is in another script but works for what you want.

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

hide gui.label after an event 1 Answer

Score display not working 1 Answer

Score/Points when player kills enemy (multiplayer) 1 Answer

GUILabel Text Overlapping After Updating 0 Answers

how can i flip my scoredisplay var guitext so that it appears upside down? 0 Answers


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