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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Scripting but kind of math question [UI Slider]

Discussion in 'Scripting' started by i3artyy2222, Nov 11, 2015.

  1. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Hi all,
    So im wondering because i have a slider bar where 1 = 100% of the slider bar and 0 = 0%
    So i have script that calculates all objects in scene with tag ''points'', there could be 6 objects or even 350, any number of objects.
    The problem is that i dont know how to make slider bar so it shows for example always full 100% even that there are 5 objects in scene.
    Here my variables
    Code (JavaScript):
    1.  
    2. var NumberOfPointsInStart: int = 0;
    3. var CurrentPoints : int = 0;
    4.  
    first as the name says it calculates all the number of points in the scene at function start
    Second in function start is = to NumberOfPointsInStart and shows current points
    When you destroy point the number goes down in current points but NumberOfPointsInStart stays always same from begining till end.
    So i have code that updates every frame for slidebar but i dont know how to make it NumberOfPointsInStart = to 100% of current points :? [and the problem is i could research in google but idk how to put this in simple short words]

    Thanks for helping !
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    So you want the bar to start at 0% and increase to 100% as you destroy stuff?
    Code (csharp):
    1.  
    2. (NumberOfPointsInStart - CurrentPoints) / NumberOfPointsInStart;
    3.  
     
    Kiwasi likes this.
  3. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Hi there i want other way around start at 100% and when get destroyed get to 0

    looks like this code isnt working i dont know why :/ i know when i make
    .fillAmount = CurrentFireToDestroy / 100f;
    then it shows for example when there is 21 objects it shows as 21% and when i destroy one it goes down so the bar is working
     
    Last edited: Nov 11, 2015
  4. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    I made it :D took my calculator out played around a bit with sheet of paper and pen and i did it
    This is what i wrote currentpoints/pointsatstart x 100 / 100
     
  5. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Code (JavaScript):
    1. (CurrentPoints / StartPoints * 100f) / 100f;
    If anyone needed
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    The 100's are redundant then. All you need is
    Code (csharp):
    1.  
    2. CurrentPoints / StartPoints;
    3.  
    Edit: It occurs to me why you might have been running into issues. If both of those are int then the result is int which won't contain decimal information. So you'd want to cast each to float and make sure the result is a float as well

    Code (csharp):
    1.  
    2. float percent = (float)CurrentPoints / (float)StartPoints;
    3.  
     
    Last edited: Nov 11, 2015
    Kiwasi and i3artyy2222 like this.
  7. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Thanks for help