[URGENT!] Unity crashing when I level up my character in game?
Hey guys, I am making an RPG, I had this working before, I haven't changed anything so I'm a bit stumped. Basically when I get exp my exp fill bar fills up, then upon leveling it increases my level, updates the EXP I need for next level and that is that;
However, upon setting up my first quest, my game/unity crashes when I level up. I have the Editor.log Here but I have no idea what I'm actually looking for in here, any help would be super appreciated!
I'm also unsure what code I should provide let me know what you need and I'll post it, I'm thinking it's the level up event it's self so you can find that below;
public void GetExp(float exp) { experience += exp; float xpNeeded = GameInfo.ExperienceForNextLevel(Level*10); float previousExp = GameInfo.ExperienceForNextLevel(Level - 1); while (experience >= xpNeeded) { LevelUp(); xpNeeded = GameInfo.ExperienceForNextLevel(Level); previousExp = GameInfo.ExperienceForNextLevel(Level - 1); } experienceBar.Find("fillBar").GetComponent<Image>().fillAmount = (experience - previousExp) / (xpNeeded - previousExp); Debug.Log("EXP TO NEXT LEVEL : " + xpNeeded.ToString()); CurrentEXP = experience; RequiredEXP = xpNeeded; } void LevelUp() { level++; levelText.text = "Level : " + level.ToString(); }
Any suggestions welcome or even if you guys could point me in the right direction of reading the log, this is my biggest project yet so are still somethings I'm new to xD
Have a nice day :)
In case it is caused by this it is because of the while and probably the xpNeeded is getting "wrong" value thus experience remains greater than it forever! To debug it use something like this->
int safeCount = 0;
while (experience >= xpNeeded)
{
Debug.Log("xp: "+experience + " xpNeeded: " + xpNeeded);
LevelUp();
xpNeeded = GameInfo.ExperienceForNextLevel(Level);
previousExp = GameInfo.ExperienceForNextLevel(Level - 1);
safeCount++;
if (safeCount == 10) //this will stop the while on 10 iterations you may change the value along your needs
break;
}
Also consider to cache the reference of the fillBar ins$$anonymous$$d of using the Find each time.
Works now thanks, guess it was an endless loop or wrong values like you said.. I will looking into cache for the fillBar too thanks absolute life saver! :D
I think the wrong value was that I was calling myLevel which I had set to 1 rather than to the playerLevel from the baseStats if that makes sense, lol, Thanks again dude!
Your answer
Follow this Question
Related Questions
How to fix snake behavior in Unity3d? 0 Answers
How can I make this character move smoothly? 1 Answer
Navigation in VR Mode?!! 0 Answers
Unity3d Character Spawn Point 0 Answers