The problem is fixed!
Level up system problem
Hello there!
Right now I'm working on a level up system script but I've encountered a problem.
When I get XP, the XP bar moves outside the box (look at the image). When I'm level 1 it works fine, but when the XP amount increases when I'm level 2 or more the XP bar is acting strange.
It should be so when the amount of XP that is required to level up increases, the amount of XP that adds to the XP bar should look smaller, but now it adds the exact same amount (same width), so that's why It goes outside the box.
Does anyone know how to fix this problem?
Script
var guiSkin : GUISkin;
var levelTexture : Texture;
var levelBGTexture : Texture;
var levelUpSound : AudioClip;
var currentXP : float = 0;
var maxXP : float = 100;
var level : float = 1;
private var leveledUp : boolean = true;
var GUIControllerScript : GUIController;
function Update ()
{
if(currentXP >= maxXP)
{
levelUpSystem();
}
if(leveledUp)
{
leveledUp = false;
}
}
function levelUpSystem ()
{
currentXP = 0;
maxXP = maxXP + 50;
level++;
AudioSource.PlayClipAtPoint(levelUpSound, transform.position);
leveledUp = true;
}
function OnGUI()
{
GUI.skin = guiSkin;
GUI.depth = 0;
if(GUIControllerScript.GUIOn)
{
GUI.DrawTexture(new Rect((Screen.width / 2) - 165, 20, 300, 30), levelBGTexture);
GUI.DrawTexture(new Rect((Screen.width / 2) - 165, 20, 3 * currentXP, 30), levelTexture);
GUI.Box(new Rect((Screen.width / 2) - 165, 20, 300, 30), currentXP + "/" + maxXP, "level");
GUI.Box(new Rect((Screen.width / 2) - 335, 20, 300, 30), level + "");
}
}
The GUI system you are using is deprecated and complex to implement correctly. You should use the new UI system as it would make your intended action a whole lot simpler.
$$anonymous$$aybe a something better to say would be: which version of unity are you using OP, and why? Perhaps he or she doesn't want to upgrade and break his code/deal with the current issues.
[edit] On a second thought: perhaps you ought to exa$$anonymous$$e your mathematics on the placement and scaling of your textures. It may just be a simple arithmetic or multiplication issue. I encourage you to start from the beginning of your code doing the math by hand. You will probably find the problem that way. I've got a pretty good indication that the problem is related to your currentXP variable, and how you give the drawTexture function parameters.
Lastly, this is a semi good question, because you provided both a screenshot and some code. In the future please try to solve things yourself, or you will stunt your own personal growth as a programmer! We haven't seen any indication that you've actually tried to solve this yourself (none that you've mentioned at least).
I'm using Unity 5.0, so I can use the new UI system if I want, but all of my scripts have the old system so I think I will stick with that right now. I've tried some things to solve this but I haven't managed to fix it yet.
Scripting isn't my best feature, I'm more of a designer :)