- Home /
Shorten C# Script
I am working on create a leveling system I have this one created but it horribly long is there any way I could shorten this code down? I like how it works. Could I use an array and make it so you can type in the exp and amount of level in the inspector instead incase I decide I only want 10 levels instead of 80?
using UnityEngine;
using System.Collections;
class PlayerStatus : MonoBehaviour
{
public int curExperience = 1;
public int curLevel = 1;
public int lvl_1 = 10;
public int lvl_2 = 20;
public int lvl_3 = 30;
public int lvl_4 = 40;
public int lvl_5 = 50;
public int lvl_6 = 60;
public int lvl_7 = 70;
public int lvl_8 = 80;
public int lvl_9 = 90;
public int lvl_10 = 100;
public int healthlvl_2 = 20;
public int healthlvl_3 = 30;
public int healthlvl_4 = 40;
public int healthlvl_5 = 50;
public int healthlvl_6 = 60;
public int healthlvl_7 = 70;
public int healthlvl_8 = 80;
public int healthlvl_9 = 90;
public int healthlvl_10 = 100;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
if(curLevel <1)
curLevel = 1;
if(curLevel >80)
curLevel = 80;
switch(curLevel){
case 0:
if(curExperience <= lvl_1){
curLevel += 0;
}
break;
case 1:
if(curExperience >= lvl_2){
curLevel += 1;
playerHealth.playerMaxHealth += healthlvl_2;
}
break;
case 2:
if(curExperience >= lvl_3){
curLevel += 1;
playerHealth.playerMaxHealth += healthlvl_3;
}
break;
case 3:
if(curExperience >= lvl_4){
curLevel += 1;
playerHealth.playerMaxHealth += healthlvl_4;
}
break;
case 4:
if(curExperience >= lvl_5){
curLevel += 1;
playerHealth.playerMaxHealth += healthlvl_5;
}
break;
case 5:
if(curExperience >= lvl_6){
curLevel += 1;
playerHealth.playerMaxHealth += healthlvl_6;
}
break;
case 6:
if(curExperience >= lvl_7){
curLevel += 1;
playerHealth.playerMaxHealth += healthlvl_7;
}
break;
case 7:
if(curExperience >= lvl_8){
curLevel += 1;
playerHealth.playerMaxHealth += healthlvl_8;
}
break;
case 8:
if(curExperience >= lvl_9){
curLevel += 1;
playerHealth.playerMaxHealth += healthlvl_9;
}
break;
case 9:
if(curExperience >= lvl_10){
curLevel += 1;
playerHealth.playerMaxHealth += healthlvl_10;
}
break;
}
}
}
Answer by clunk47 · Aug 14, 2013 at 05:13 PM
EDIT: Here is a test that will allow you to assign the array values in the inspector for Health Levels. You don't need an array for Levels, you can just use an int for current level. In this example, press keypad plus '+' to increase the current level. This will debug the current level, and the max health for that level.
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
public int[] healthLevels;
int currentLevel = 0;
int maxHealth;
void Update()
{
if(currentLevel < healthLevels.Length - 1)
{
if(Input.GetKeyDown(KeyCode.KeypadPlus))
{
currentLevel++;
maxHealth = healthLevels[currentLevel];
}
Debug.Log ("Current Level - " + currentLevel + " / Max Health - " + maxHealth);
}
}
}
I'm not sure I am following. The declaration of the levels is set up to tell how much exp is required to level and the health declaration is designed to tell how much health to add if you are on this level.
Answer by roojerry · Aug 14, 2013 at 05:39 PM
Other than level 80, for some reason, all your values are just 10 times the level. Why not just scrap all the exp and health variables and do something like:
curLevel = (int)curExperience/10;
playerHealth.playerMaxHealth += curLevel * 10;
Your answer