# [Solved] Simple equation not working on slider?

Got a script that manages players experience and shows it on a slider. Each new level needs 25xp more than the previous one and the slider value is set 0-100.
For the first level, 25xp is needed, 50 for 2nd lvl etc… it’s all good until my character reaches level 5 (needs 125xp) so the “sliderMultiplier” variable is less than 1 (becomes a decimal number). My experience slider stops updating at all, but still I can level up. I think it’s treated as a 0 all the time, tried to workaround and it’s just getting irritating because I think I’m missing a simple solution here.

This is the script:

PlayerHealth playerHealth = new PlayerHealth();
public GameObject LevelUp;
public GameObject LevelUpUI;
public Slider experienceSlider;
//public AudioClip levelUpSound;

public static int playerExperience = 0;
public static int playerLevel = 1;
public int xpPerLevel = 25;
private static int neededXpForLevel = 25;

void Start ()
{

}

void Update ()
{
float sliderMultiplier = 100 / neededXpForLevel;
experienceSlider.value = sliderMultiplier * playerExperience;
if (playerExperience > neededXpForLevel)
{
playerHealth.PlayParticleEffect(LevelUp, 0);
playerLevel++;
playerExperience -= neededXpForLevel;
neededXpForLevel += xpPerLevel;

}
}

If you always want your slider to be between 0 and 100 percent, set the slider value to:

(playerExperience / neededXpForLevel) * 100f

" (playerExperience / neededXpForLevel) " will give you a number between 0 and 1, and then you multiply by 100 to make it from 0 to 100.

More logical than my approach for sure, but yet now it doesn’t update at all. I have a feeling that for some reason all decimal numbers are treated as 0.

Oh it’s because your variables are all int type. Make them floats in order to do decimal math. Right now (playerExperience / neededXpForLevel) is always truncating to 0 because it’s an int.

1 Like

I just had a feeling that something stupid must be the reason xd Thank you!

1 Like