- Home /
Variable changes to a negative number somehow
Hello people of Unity Answers! I basically want to get my Level and XP system to work properly. I mean.. It works very well but I cant find the one logic error. As you can see in the script, I have coded it very simple (cuz my programming skill is not good lol) and it has only one Error. This is how the one part should be: If you level up, your current XP gets subtracted by the XP-requirement (So if the XP is larger then the required XP, the rest stays). The problem is, that if the player levels up, the XP changes to a negative number (Even if the XP is larger than the required XP). It somehow subtracts the XP twice or something... Please Help me and please tell me, how to find bugs easier. I always use Debug.Log(), are there any better techniques or something?
private WallController wallController;
private PlayerStats playerStats;
public float AllCollectedXp;
public bool CanLvlUp;
public static LevelAndXpManager instance;
void Start()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(this);
}
else if (instance != null)
{
Destroy(gameObject);
}
playerStats = FindObjectOfType<PlayerStats>().GetComponent<PlayerStats>();
}
void Update()
{
if (CanLvlUp)
{
LvlUp();
CheckIfCanLvlUp();
}
}
public void IncreaseCollectedXp(float Xp)
{
AllCollectedXp += Xp;
}
public void CheckChangeSetLvlAndXp()
{
ChangePlayerXp();
CheckIfCanLvlUp();
if (CanLvlUp)
{
LvlUp();
}
}
public void ChangePlayerXp()
{
playerStats.playerXp += AllCollectedXp;
AllCollectedXp = 0;
PlayerPrefs.SetFloat("playerXp", playerStats.playerXp);
}
public void CheckIfCanLvlUp()
{
if (playerStats.playerXp >= playerStats.XpToLvlUp)
{
CanLvlUp = true;
}
else CanLvlUp = false;
}
public void LvlUp()
{
playerStats.playerXp -= playerStats.XpToLvlUp;
Debug.Log(playerStats.playerXp);
playerStats.playerLvl++;
playerStats.XpToLvlUp += 50;
PlayerPrefs.SetInt("playerLvl", playerStats.playerLvl);
PlayerPrefs.SetFloat("playerXp", playerStats.playerXp);
PlayerPrefs.SetFloat("XpToLvlUp", playerStats.XpToLvlUp);
}
}
Answer by DanielGL · Jan 18, 2021 at 07:39 PM
I guess the problem is because you are calling the LvlUp method in the Update and in the CheckChangeSetLvlAndXp method, also, your code are a little bit messy, I made a sample of a level controller with some hints of how do you can debug your code in best ways, try modify that for works in that you need and, if you have any questions just ask ;) Sample code:
using UnityEngine;
public class LevelControlerSample : MonoBehaviour
{
public int xpToLevelUp = 50;
public int xp;
public int level;
//Hints for debug, you can create a area like that for throw your debug code
#region DebugArea
public int myXpPoints;
/*The ContextMenu let you call this method in the inspector(right click in your class),
without start the game, with this you can debug if your xp is increasing correctly*/
[ContextMenu("AddMyXpPoints")]
void AddMyXpPoints()
{
AddXP(myXpPoints);
}
#endregion
public void AddXP(int xpAmount)//Call this for add more xp
{
xp += xpAmount;
if (CanIncreaseLevel())//The verification if it needs to up the level occurs here, so you don't need to make it on the Update method
{
IncreaseLevel();
}
}
bool CanIncreaseLevel()
{
if (xp >= xpToLevelUp) return true;
return false;
}
void IncreaseLevel()
{
xp -= xpToLevelUp;
xpToLevelUp += 50;
level++;
}
}