Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Calculating and displaying % - why doesn't this work?

Discussion in 'Getting Started' started by Ryeath, Dec 11, 2016.

  1. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    Hi again,

    Back with what should be an easy problem, but is driving me nuts.

    Trying to calculate and display percentage of pick ups collected in the Roll a Ball tutorial, but my percentages go from 0% to 100% with nothing in between.

    Code (CSharp):
    1.  
    2.  
    3. void SetCountText () // sets count, score and you win text.
    4.     {
    5.         countText.text = "Count: " + count.ToString();
    6.         scoreText.text = "Score: " + score.ToString();
    7.         if (count >= 1)
    8.         {
    9.             //winText.text = "Level Complete!";
    10.             percent = (count / 12) * 100;
    11.             percentText.text = percent.ToString() + "% of Pick Ups Collected.";
    12.            
    13.         }
    14.     }
    I have a float named percent declared earlier in the program. 12 pick ups. Should just be able to divide count (pick ups collected so far) by total pick ups (12) * 100 to get percent.

    Can't seem to figure out what I am doing wrong.

    Again, thanks in advance.
     
  2. Philip-Rowlands

    Philip-Rowlands

    Joined:
    May 13, 2013
    Posts:
    353
    The code you have there looks right to me. Does it go from 0 to 100% as soon as you collect one item?
     
  3. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    No, the display stays at 0% until I pick up the 12th item them it jumps straight to 100%
     
  4. Philip-Rowlands

    Philip-Rowlands

    Joined:
    May 13, 2013
    Posts:
    353
    Okay, so the next thing to check is if it's being called when you collect the other pickups - it probably is, but try adding a Debug.Log statement to the start of the method. If that is called every time you collect another object, add more log messages to the method.
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,841
    The problem is that "count" is an integer, and so is "12". So when you write "count / 12", the C# compiler takes this as integer division.

    Easiest way to fix it is to instead write count / 12f, making the 12 a floating-point literal. C# will then do floating-point division.
     
    kyzzer, Homicide, sean244 and 2 others like this.
  6. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    Once again Joe comes to the rescue.

    Change 12 to a float worked.
    I changed the total to 146 to match all the pickups in the level and added ("#.00") to my display string and it all works great now.

    Code (CSharp):
    1. void SetCountText () // sets count, score and you win text.
    2.     {
    3.         countText.text = "Count: " + count.ToString();
    4.         scoreText.text = "Score: " + score.ToString();
    5.         if (count >= 1)
    6.         {
    7.             //winText.text = "Level Complete!";
    8.             percent = (count / 146f) * 100;
    9.             //percent = (int)percent;
    10.             percentText.text = percent.ToString("#.00") + "% of Pick Ups Collected.";
    11.            
    12.         }
    13.        
    14.     }
    Thanks again to Joe and Philip.

    I'm still working on the Move.Towards Joe recommended. I couldn't get it to work, but looking at it again, I think I may know why now. If not I'll be back with another question. Almost ready to present my version of the tutorial for critique.
     
    Philip-Rowlands and JoeStrout like this.
  7. howler123

    howler123

    Joined:
    May 7, 2017
    Posts:
    17
    Had the same problem, was trying to get percentages using Integer math, changed vars to float and it worked. I had even tried casing the calc to float and that didn't work;
     
  8. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Debug.Log would have caught this right away.