- Home /
level up need help
hello all i got this script that iv been working one today and its got a switch function on and when i get 50 exp i made it that the level (level of character) become level 2 because of the level += 1; any way my prob is that when i get the exp, and it adds up to 50 its adds 1 level really quick so what i want it to do is to just add one level not 1 + level ever second till i go over 50 if you can help thanks
here's the script
var level = 1;
private var ExpUp = false;
static var EXP = 0;
function OnTriggerEnter (hit : Collider) { if(hit.gameObject.tag == "ExpOrb") { ExpUp = true; ExpController.EXP += 25; } }
function Update () { switch(EXP) { case 50: level += 1; break;
case 150:
level += 1;
break;
case 300:
level += 1;
break;
case 500:
level += 1;
break;
case 750:
level += 1;
break;
case 1000:
level += 1;
break;
case 1300:
level += 1;
break;
case 1600:
level += 1;
break;
case 2000:
level += 1;
break;
}
print(EXP);
print(level);
}
Answer by · Sep 09, 2010 at 03:57 AM
Don't put the level up check in Update (). If you want to take this approach, create a separate function and call it any time you gain experience.
function CheckLevelUp()
{
switch(EXP)
{
case 50:
case 150:
case 300:
case 500:
case 750:
case 1000:
case 1300:
case 1600:
case 2000:
level += 1;
break;
}
}
This isn't a particularly flexible way of handling experience or levels. For example, if you ever have anything that doesn't give 25 experience, you may miss the 'case' and the levelup would never occur. But your question was asking how to get this working, so I've tidied it up for you.
if there is other way can you tell me sorry im just a beginner and i wanted to try out the switch function
The pseudocode provided in this answer (http://answers.unity3d.com/questions/13334/experience-points-level-up-for-rpg/13369#13369) would be a cleaner way of doing it. Another more-reliable system is posted in http://answers.unity3d.com/questions/18859/experience-system-leveling-prematurely. It depends on what kind of flexibility you want/need. If you'll only ever award the experience in 25 point amounts, the way you've done it will work fine.
Your answer
Follow this Question
Related Questions
Help with level collider 2 Answers
character switching on collision 1 Answer
Internal collisions 1 Answer
My script isn't detecting OnTriggerEnter why? 1 Answer
Adding colliders to a level 0 Answers