Enemy health bars working with unity5

I know there is a lot of these kinds of questions but they are all for older versions of unity, that don’t work with unitys new UI system. The major obstacle i’m trying to get across is that I can’t use game objects or really do anything through the hierarchy for the game i’m working on.

void OnGUI()
        {
            GUI.color = Color.red;
            GUI.Label(new Rect(unitXPosition, unitYPosition, 150, 20), health + "/" + maxHealth);
        }

this is really all of the code that I am using unityYPosition and unitXPosistion just update in the update function to change there floats to the gameobjects corresponding axis value. Currently the texts moves with the gameobject but it only ever stays in the top left corner.

While you can try making the health bar a part of the enemy game character, you can probably give them an update function to rotate the bar towards the player. I would directly use a slider where you removed the interactibility and the “handle”.

Odds are that your health bar might be partially visible by the player before the enemy is visible, but you can probably solve that by using alpha to make them invisible once they have not received a hit for ~2 seconds…

Hi, as Priyanshu said, there is now a more easy way to do that.

Using GameObject/UI/Image. Chose the full image of your health bar, chose type filled, the filling method you want and you just have to make a script to update fill amount with the ennemy health.

I hope that helps.