Posted on

Unity Health Bar Tutorial – Using Fill Images

How to make a health bar

Welcome to this quick game mechanic tutorial on how to make a health bar using fill images in Unity 3D. In this lesson, we will teach you how to create and update fill meters that you can use for health bars, shield bars, stamina bars, or even experience bars.

Create a Health Bar Object in Unity

To create a fill bar all you have to do is Is create a UI image object. you then you need to set the rect transform so the health bar is positioned and sized the way you want. You then need to apply a source image to the image component so that the Image type drop-down menu becomes available. You then can set the image type to Filled which will make it so you can determine the fill amount of our health bar. You can then decide if you want your fill meter to be a linear or a radial health bar by setting the fill mode. With your health bars created we will now switch over to the code we need.

Update Health Bar with C# Code

To get your health bar to update you will need at least three variables. Two of them need to be int or float. One will be for your current health. The other will be for the max health and the last one will be for holding the image component of the. To then update the health bar you only need one line of code. You will first need to access the fill amount of the image variable and set it equal to the current health divided by the max health. This will give you a decimal value of a percentage of how much health you currently have over how much total health you could have. You will need to have this line of code right after any time you change your current health value.

Test your Work

Inside Unity, you need to make sure you set all the variables and then you can test your game. If you follow along with this video you should then have a working health bar.

Posted on

Multiple Camera in your Unity Photon Multiplayer Games ft Eletrax

If you are ever building a multiplayer first-person shooter in Unity using the Photon plugin you might run into this problem. The problem we discuss is when you have more than one camera in your scene because there is a camera attached to each player object. This will cause the cameras to stack up on top of each other and the top or primary camera might not be your first-person camera which means while you are controlling your local player object you might not be viewing your player through their camera.

To fix this problem you need to disable the camera of each player object that is not your local player object. You will need to use the photon view component to check to see if the is mine variable is false. If it is then you will need to use a camera variable and set the enable variable to false. This will make it so you will only have one camera active in your multiplayer scene and that will be your local first-person camera.

Posted on

Photon 2 Unassigned Reference Exception with Observed Components

This is a Debugging video for how to fix an Unassigned Reference Exception error when using an Observed Component in Unity with Photon. This is a very easy error to fix as It only takes switching one variable in the inspector. If you are receiving this error it is probably because you accidentally made one little mistake without realizing it.

To fix this error all you must do is look at all you Photon Prefabs and make sure that the Observe Option of the Photon View component is set to off or you have at least on observed component set. If the Observe option is turned on and you don’t have a component being observed then you will be receiving this error.