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
0
Question by Z13J · Jul 31, 2012 at 05:08 AM · c#scoregui.label

GUILabel Text Overlapping After Updating

Hey guys,

So I am trying to get my brother familiar with programming, since that is what he says he wants to do as a career. Therefore, I have set-up this simple recreation of PacMan to demonstrate moving a character, dynamically interacting with objects, and eventually adding enemy AI. Then I ran across a problem I have encountered twice before, but I forgot how I fixed - my Score and Pellets Collected GUI.Labels were updating, but leaving previous font behind. I've attached a screenshot showing what I mean - notice how Score and Pellets Collected have other numbers remaining behind them, while Pellets Remaining does not.

I currently have my GUI in my GameManager class, but it will likely be moved by the end of the project, if necessary. I use GameManager as a warehouse for scores, stats, and other variables of the sort.

 public class GameManager : MonoBehaviour {
  public int score;
  public int pelletsGot;
  public int pelletsLeft;
  public int pelletScoreValue = 10;
  
  public float timer;
  private float startTime;
  
  // Use this for initialization
  void Start () {
  startTime = Time.time;
  }
  
  // Update is called once per frame
  void Update () {
  timer = Time.time - startTime;
  
  
  pelletsLeft = GameObject.FindGameObjectsWithTag("Collectible").Length; // Counts the number of pellets
  }
  
  void OnGUI(){
  GUI.Label(new Rect(10, 10, 100, 50), "Score: " + score.ToString());
  GUI.Label(new Rect(10, 65, 100, 50), "Pellets Collected: " + pelletsGot.ToString());
  GUI.Label(new Rect(10, 120, 100, 50), "Pellets Remaining: " + pelletsLeft.ToString());
  }
  
  public void CollectPellet(){
  score += pelletScoreValue;
  pelletsGot++;
  }
 }

The problem is on the Score and Pellets Collected GUI.Labels, and not the Pellets Left GUI.Label. I have a feeling this is because pelletsLeft is being updated in the Update method, while Score and Pellets Collected are only being updated in the OnGUI method.

If I am correct, how would I work around this? I am a new game developer myself, but I wanted to show my brother what was possible with less than a year of experience. I don't know all the tricks yet, so any tips are welcome.

And yes, I have checked to see if this has been asked before, which I found nothing of use. I have spent the last 2 hours pondering over this. Hopefully some one in the community has the answer.

Thanks in advance. alt text

screen shot 2012-07-30 at 9.01.03 pm.png (22.0 kB)
Comment
Add comment · Show 2
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 Mizuho · Jul 31, 2012 at 05:48 AM 0
Share

Try not using the .ToString() method. Integer variables are automatically converted to strings if attached to a string. Finding your pellets with a FindGameObjectsWithTag() call in Update() is asking for trouble.

Your brother shouldn't be too concerned; I've only been working with Unity for about 2 months, and I'm well ahead of the design $$anonymous$$m (which consists of 3 people) while working solo. I hadn't even written anything in C# before this.

avatar image fafase · Jul 31, 2012 at 06:00 AM 0
Share

Your variable pelletLeft, is it changing all the time? Find functions should be avoided in the Update, you would better place it in the Start function and then place it in an array from which you can get the length. Then in the game you only refer to that array.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by JChilton · Jul 31, 2012 at 06:35 AM

Check the clear flags on your camera. If not set, they'll continually render over themselves. (no clear screen calls)

Comment
Add comment · Show 5 · 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 Z13J · Jul 31, 2012 at 03:53 PM 0
Share

Checked the clear flags on my camera, it was set to Skybox. I don't typically alter that setting, so I had a feeling it wasn't the problem. Is there a certain script to attach to my camera gameobject that would help?

avatar image JChilton · Jul 31, 2012 at 04:43 PM 0
Share

Only other thing I can think of is that you have this same OnGUI on another script. The issue here is that it's drawing over itself. Either because it's not clearing the screen after each draw, or you're drawing twice. Check other scripts in the scene and make sure you don't have repeat OnGUI code possibly?

avatar image Z13J · Aug 01, 2012 at 03:54 AM 1
Share

Unfortunately that is not the problem either - I have only used OnGUI in this one class.

EDIT: I solved the problem, I had the script attached to a different GameObject as well, meaning it would render GameObjectA's score, which was correct, and GameObjectB's score, which was incorrect, and the same time. JChilton was correct, I just misinterpreted what he was saying. Thanks J!

avatar image NickP_2 · Jul 13, 2013 at 11:10 PM 0
Share

One year later, you ended my 2 hour search ! double scrip attached ! Thanks !

avatar image etbrachium · Jun 09, 2014 at 04:12 AM 0
Share

It's now June 2014, and I'm happy that this QA saved me lots of time debugging this, same issue, one script with a single OnGUI method, applied to multiple objects (prefabs).

Thanks!

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

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How do i save several scores in several scripts? 0 Answers

Make a custom score counter in unity with c# 1 Answer

Guitext for score over time. 2 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