• 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 czajenczini · Jul 16, 2016 at 06:55 PM · c#score systemproblema

My FinalScore Counter adds up to infinity, why?

Why is the variable WinnerScore going crazy? It keeps adding up to infinity. Could sb help me? Here is the code:

void OnGUI() { GUI.skin = skin; GUI.Label(new Rect(40, 200, 2, 2), "Collected Coins: " + tokenCount + "/" + TotalTokenCount.ToString(), skin.GetStyle("Coin")); GUI.Label(timerRect, currentTime, skin.GetStyle("Timer")); if (startTime < 5f) { skin.GetStyle("Timer").normal.textColor = WarningColor; } else { skin.GetStyle("Timer").normal.textColor = CustomColor; }

     if (ShowWinBox)
     {
         Rect WinScreenRect = new Rect(Screen.width / 2 - (winScreenWidth / 2), Screen.height / 2 - (winScreenHeight / 2), winScreenWidth, winScreenHeight);
         GUI.Box(WinScreenRect, "Click continue or press space to load the next level");
         float tokens = (float)tokenCount;
         int currentScore = (int)tokens * (int)startTime;

         **WinnerScore = WinnerScore + currentScore;**


         GUI.Label(new Rect(Screen.width / 2 - (winScreenWidth / 2) + 10, (Screen.height / 2) - 80, 100, 25), "you've finished with " + startTime + " seconds left", skin.GetStyle("Box"));
         if (tokens==1)
         {
             GUI.Label(new Rect(Screen.width / 2 - (winScreenWidth / 2) + 10, (Screen.height / 2) - 55, 50, 25), "you've collected " + tokens + " coin", skin.GetStyle("Box"));

         }
         else
         {
             GUI.Label(new Rect(Screen.width / 2 - (winScreenWidth / 2) + 10, (Screen.height / 2) - 55, 50, 25), "you've collected " + tokens + " coins", skin.GetStyle("Box"));

         }
         GUI.Label(new Rect(Screen.width / 2 - (winScreenWidth / 2) + 10, (Screen.height / 2) - 30, 100, 25), "your score is: " + currentScore, skin.GetStyle("Box"));

         if (SceneManager.GetActiveScene().buildIndex == 8)
         {
             GUI.Label(new Rect(Screen.width / 2 - (winScreenWidth / 2) + 10, ((Screen.height / 2) - 5), 100, 15), "your final score is: " + WinnerScore, skin.GetStyle("Box"));
         }

}

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 czajenczini · Jul 16, 2016 at 07:41 PM 0
Share

You are right, I'm gonna place this equation somewhere else. Thank you, this was my 1st question on this site, I'm so glad there are so many helpful people here

avatar image wojtask12 · Jul 16, 2016 at 07:46 PM 0
Share

You totally should consider using new UI system, it's really great. Here's some nice tuts https://unity3d.com/learn/tutorials/topics/user-interface-ui

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ScaniX · Jul 16, 2016 at 07:33 PM

The OnGUI() method is called repeatedly like the Update(), so you have to make sure to only execute the code that changes the score once.

At least in the posted code I cannot see anything that sets tokenCount or startTime to zero, so it will add those values to your score on every gui draw.

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 MegaWave_ · Jul 16, 2016 at 07:14 PM

I can't really see what is wrong here but maybe

WinnerScore = WinnerScore + currentScore;

is called more times than it should and it goes like:

 currentScore = 1
 +
 WinnerScore = 1
 =
 WinnerScore = 2

then it goes

 currentScore = 1
 +
 WinnerScore = 2
 =
 WinnerScore = 3

and so on.

Comment
Add comment · Show 4 · 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 czajenczini · Jul 16, 2016 at 07:22 PM 0
Share

How should I add it then? (currentScore variable works fine) Btw thank you so much for the response

avatar image MegaWave_ czajenczini · Jul 16, 2016 at 07:28 PM 1
Share

I don't know but you should make something like if (scoreCounted = false) { WinnerScore = WinnerScore + currentScore; scoreCounted = true; } This might be a bad way to do it since I am not the best, but I hope it works :)

avatar image czajenczini MegaWave_ · Jul 17, 2016 at 09:45 AM 0
Share

It worked, I've also changed WinnerScore to static :)

avatar image czajenczini · Jul 16, 2016 at 07:29 PM 0
Share

I'll try it, thank you :)

avatar image
0

Answer by wojtask12 · Jul 16, 2016 at 07:35 PM

Why don't you use NGUI instead of the old one? I guess you've got this code inside of OnGUI function which can be called multiple times in a second (read here https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnGUI.html), so it adds the currentScore on each call - which leads to infinity

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

score counter is not accurate 1 Answer

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Simple collision function acting strangely 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