Best way to assign health according to level progress?

Hi guys,

in my game I have a building. You can upgrade this building to get higher level of this building. But according to different levels I want to assign different health.

Now I have this:

public float level1Health;
public float level2Health;
public float level3Health;
private float health;

void Start()
{
	int currentLevel = PlayerPrefs.GetInt(gameObject.name, 1);

	switch(currentLevel)
	{
	case 1 :
		health = level1Health;
		break;
	case 2:
		health = level1Health;
		break;
	case 3:
		health = level1Health;
		break;
	}
}

This works fine. But each building have different number of levels. So I need to have an Array:

public float levelHealth;

How do I do the switch than or how do I assign each index? What I need is to dynamicly generate the switch or something like that.

You’re overthinking it.

If you use an array for levelHealth, it will actually be considerably easier to assign the value based on the level number variable itself.

int currentLevel = PlayerPrefs.GetInt(gameObject.name, 1);
health = levelHealth[currentLevel];

Edit: Just make certain that you will never exceed the length of the array. You could instead use something like health = levelHealth[(int)Mathf.Min(currentLevel, levelHealth.length - 1)]; if you want a freely defined maximum value.

Edit 2: Whoops, shouldn’t type stuff like that when I’m tired. Fixed a few errors.