Quick implementation of multiple types of enemy health bars

Hey, so I’m sure we’ll get into this later but I figured I’d give it a shot now. To support multiple enemy types, I created a new enum in our utility.cs:

public enum EnemyType
{
    Easy,
    Medium,
    Hard
}

I kept it simple with easy/medium/hard for now.
In the Enemy.cs script, I serialized three bools for easy, medium and hard (I’m sure there’s a better way than this) and created a new method:

public EnemyType GetEnemyLevel()
    {
        if (easy)
        {
           return EnemyType.Easy;
        }
        else if (medium)
        {
            return EnemyType.Medium;
        }
        else
        {
            return EnemyType.Hard;
        }
    }

From there, I serialized three new Texture2D textures in the EnemyHealthBar.cs script for the three levels, and added this switch statement into the Start() method:

        switch (enemy.GetEnemyLevel())
        {
            case EnemyType.Easy:
                healthBarRawImage.texture = easyHealthBar;
                break;
            case EnemyType.Medium:
                healthBarRawImage.texture = mediumHealthBar;
                break;
            case EnemyType.Hard:
                healthBarRawImage.texture = hardHealthBar;
                break;
            default:
                Debug.LogError("Error in enemy health bar");
                return;
        }

Did anyone else implement this in another way? I didn’t put a whole lot of time into it but hey, it works for now.

1 Like

You could use an EnemyType enum member variable instead of the boolean ones but this is neat! :+1:

Privacy & Terms