I don't know if you will see this, but when I change the current health, the fill goes to either 1 or 0 instead on .9, .8, .7 ect. here is the code for the fill bar status: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class FillStatusBar : MonoBehaviour { public PlayerHealth playerHealth; public Image fillImage; private Slider slider; void Awake() { slider = GetComponent (); } void Update() { float fillValue = playerHealth.currentHealth / playerHealth.maxHealth; slider.value = fillValue; } } I also have the PlayerHealth script: using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerHealth : MonoBehaviour { public int currentHealth; public int maxHealth; } I have been stuck on this for a while now, so if you could help that would be great.
i made the player script with everything right, but forgot to make it a float. i spent way too long wondering why it wasnt working. Thanks for the video!
I'm watching the whole playlist and loving it for the simplicity! Could you explain more about the awake() function and when to use that vs the start() method?
@@BMoDev I spent a long time trying to figure it out. I followed your first health video which declared them as Ints first . I wasnt aware that they were supposed to be floats in your follow up video, I found the answer in one of the comments. I appreciate the reply, it has been fixed although when I change the current health value to no number and then change it back to a valid number the healthbar does not go back to the correct position
Thanks for a great, simple tutorial. I'm making a game and having many different mechanics, scripts, objects, I'm looking for simple solutions to combine everything into one. You did it great!
hey bmo do you know why when clicking play my bar dissapears? exactly talking abt min 3:06 (my game is set on the bottom left corner and the slider too
I have a question but I have an object in my game like a floor spike which is supposed to have my player's health bar degenerate a bit of health, do i need to add an onTriggerEnter2D next to it because the floor spikes have the box collider 2D?
thanks for the tutorial, currently trying to use an Image like a face to indicate the remaining hp. but for some reason i am unable to get it working with the hp slider. the slider updates fine but the image doesnt change when hp is < MAXHP. if i could get your help id be most thankful
Can someone explain how I can add here that life is increased by taking a boost? The video from this channel on this topic did not help me, and I'm tired for another hour and still something is wrong. I'm trying to do something like: if the player touches an object with a booster tag, slider +1. But there is a mistake somewhere in my thinking.
im gettting error code cs0246 on the line that says "public PlayerHealth playerHealth" anyone know how to fix this? hoping i get a fast reply cause this was 4 years ago
Do you have another script called 'PlayerHealth' like the guy in the video? If you have a script for your player's health with a different name , you may get an error.
public float health = 100f; public float damage = 25f; void OnCollisionEnter2D(Collision2D other) { if (other.gameObject.CompareTag("Enemy") { health -= damage; if (health
Come on how can this video be underrated, maybe gamedevs don't like jokes? (If you have seen a Unity's video where two people try to joke while teaching, gets the same reaction, I feel like gamedevs don't like jokes?)
how do you make a health bar that goes down if you collide with something? I'm trying to make this game and I just cant find a way to make my player lose health if it collides with an enemy object. Plz help
Well in this video its hardcoded.. the short answer would be to check if your player health is equal to 0 and then stop your enemy AI from attacking the player... maybe re-enter patrol mode or something.
It's probably because you are using int variables for your maxHealth and currentHealth. Integer can't store decimal values, they will automatically be rounded down. So 60/100 would equal 0 instead of 0.6. Try using float instead.
@@DerCriticer your solution fixed it for me, but in his tutorial he used integers and it worked. how does that make sense? I followed it exactly like he said and i got the same issue that Lyuboslav had
I'm not sure if you'll see this, but hopefully you do. But I'm having a bug where the slider only works properly for max health and nothing else, do you know how to fix this?
@@heinkelion thank you soooo much. In my playerHealth script I used public int instead of public float and because of it as soon as my player took damage the health bar dropped to 0
When starting out especially its great to utilize the tools Unity provides for us! I can definitely show how this is done, the quick overview is when the arrow collides with the player it makes a call of GetComponent() and subtracts 1 from the currentHealth!
So I got it to work, but I will like to get it to work whenever an enemy shoots me or collides into me, it reduces my health. I was thinking to add it in this script yet I was getting an error: nullRefrenceException object reference not set to an instance of an object. public class DestroyByContact : MonoBehaviour { public GameObject enemyExplosion; public GameObject playerExplosion; public PlayerHealth playerHealth; public int scoreValue; private GameController gameController; private void Start() {
GameObject gameControllerObject = GameObject.FindWithTag("GameController"); if (gameControllerObject != null) { gameController = gameControllerObject.GetComponent(); } else { Debug.Log("Cannot find 'GameController' script"); } } private void OnTriggerEnter2D(Collider2D other) { // Name of the given object that collided with the enemy var name = other.tag; // if the name of the tag is the same as to Boundary tag // return and do nothing if (name == "Boundary" || name == "Enemy") { return; } if(enemyExplosion != null) { Instantiate(enemyExplosion, this.transform.position, this.transform.rotation); } if (name == "Player") {
Hello, great tutorial! I'm trying to make it automatically take one away from the playerHealth when the player gets hit by an enemy, but it's not working as it's a float. Does anyone know how to get around this?
@@BMoDev No, sadly it's not working: here's the error: error CS0019: Operator '-=' cannot be applied to operands of type 'PlayerHealth' and 'float' and here's the code: public int fillValue; public PlayerHealth playerHealth; public void OnTriggerEnter2D(Collider2D col) { if (col.gameObject.tag == "Hurt") { playerHealth -= 1f; } } }
Ah I see, Ok So PlayerHealth is a class, you need to access the variable inside of the class. Not sure how your project is setup, but you can create a new script called PlayerHealth, and inside that just add two variables public class PlayerHealth: MonoBehaviour { public float currentHealth; public float maxHealth; } and attach it to the Player. If you click on your player, in this script you'll see these variables appear and you can set values to them. ---- then, back in your HealthBar script, use playerHealth.currentHealth -= 1; let me know if you get it working!
@@BMoDev Just got it working - thank you so much! Incredibly helpful tutorials and you put so much work into helping others, one of the most useful game dev tutorial accounts i know. Keep it up!!
Are you using integers instead of floats? My first guess is the integers (which won't allow decimals) is messing up the math. If you are, try using floats
ayyy thanks for the support! This was something brought up so in my more recent videos I have the font zoomed in on, it should be better going forward :)
PlayerHealth is a script attached to my Player GameObject. It just has two float variables within it : public float maxHealth; public float currentHealth; There's nothing really to it which is why I didn't show it, you'll have to create a health system for your project to work with the health bar.
I want it to have 3 colors tho, but it will only go green and yellow, and it will never reach the 3rd else if, pls help void Update() { if (slider.value slider.minValue && !fillImage.enabled ) { fillImage.enabled = true; } float fillValue = liv.CurrentHealth / liv.MaxHealth; if (fillValue >= slider.maxValue / 1.7) { fillImage.color = Color.green; } else if (fillValue < slider.maxValue / 1.7) { fillImage.color = Color.yellow; } else if(fillValue
PlayerHealth script just holds 2 floats, no functionality: public class PlayerHealth : MonoBehavior { public float currentHealth = 10; public float maxHealth = 10; } let me know if you need help :)
The script has 2 float variables public class PlayerHealth : Monobehavior { public float currentHealth; public float maxHealth; } Are you able to get it working?
It may not look stunning, but you can create a working health bar in less than 5 minutes! Not bad
I don't know if you will see this, but when I change the current health, the fill goes to either 1 or 0 instead on .9, .8, .7 ect. here is the code for the fill bar status:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FillStatusBar : MonoBehaviour
{
public PlayerHealth playerHealth;
public Image fillImage;
private Slider slider;
void Awake()
{
slider = GetComponent ();
}
void Update()
{
float fillValue = playerHealth.currentHealth / playerHealth.maxHealth;
slider.value = fillValue;
}
}
I also have the PlayerHealth script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerHealth : MonoBehaviour
{
public int currentHealth;
public int maxHealth;
}
I have been stuck on this for a while now, so if you could help that would be great.
wow I actually understood this as a beginner, thank you :)
This video helped alot. I was searching literally for 2hrs. Thank you for making this video
You're a legend mate, your entire channel makes bringing our ideas to life so easy even when people are just starting out. Keep it up ❤️
Plz do more of the 5 minute videos.
Love your work!!!
i made the player script with everything right, but forgot to make it a float. i spent way too long wondering why it wasnt working. Thanks for the video!
HAHAHAHHAHH SAMEEEEE
I come here now and then just for the fun part in the beginning!
I'm watching the whole playlist and loving it for the simplicity!
Could you explain more about the awake() function and when to use that vs the start() method?
this was the only tutorial that worked for me
WOW this seems to be more better than using SetMax and Health methods.
thanks for this, love you brother man
💗
11/10 however I demand more subscribe party at the end of this video. perhaps a swarm of knight subscribers to storm the castle?
Thanks bmo,
Jake
I'll make it up to you in a future health bar video :)
I followed your both tutorials to what you exactly done but when i adjust the health of the player the health bar goes instantly to 0? unsure why
In your health script try switching from ints to floats
@@BMoDev I spent a long time trying to figure it out. I followed your first health video which declared them as Ints first . I wasnt aware that they were supposed to be floats in your follow up video, I found the answer in one of the comments. I appreciate the reply, it has been fixed although when I change the current health value to no number and then change it back to a valid number the healthbar does not go back to the correct position
@@BMoDev it worked thanks
Finally becoase you made this tutorial i can continue my game project Thank you!!!
Thats awesome, good luck!
İ cant find the player health script, and idont see it on the video
whats the script for the player health?
Thanks for a great, simple tutorial. I'm making a game and having many different mechanics, scripts, objects, I'm looking for simple solutions to combine everything into one. You did it great!
hey bmo do you know why when clicking play my bar dissapears? exactly talking abt min 3:06 (my game is set on the bottom left corner and the slider too
I have a question but I have an object in my game like a floor spike which is supposed to have my player's health bar degenerate a bit of health, do i need to add an onTriggerEnter2D next to it because the floor spikes have the box collider 2D?
is the player health script the same health script as the other video?
thanks for the tutorial, currently trying to use an Image like a face to indicate the remaining hp. but for some reason i am unable to get it working with the hp slider. the slider updates fine but the image doesnt change when hp is < MAXHP. if i could get your help id be most thankful
Can someone explain how I can add here that life is increased by taking a boost? The video from this channel on this topic did not help me, and I'm tired for another hour and still something is wrong.
I'm trying to do something like:
if the player touches an object with a booster tag, slider +1. But there is a mistake somewhere in my thinking.
im gettting error code cs0246 on the line that says "public PlayerHealth playerHealth" anyone know how to fix this?
hoping i get a fast reply cause this was 4 years ago
Do you have another script called 'PlayerHealth' like the guy in the video? If you have a script for your player's health with a different name , you may get an error.
Great video!
how do i make something damgage the player? i watch all the comments tried the solutions you gave them and did not work :(
public float health = 100f;
public float damage = 25f;
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("Enemy")
{
health -= damage;
if (health
@@BaconJeezbrgr Thanks a lot!
I was hoping for this code for a looong time and i finally got it!
@@UNknown-xk1hh if u are using bullets to hurt the player then u must use a line cast to check the collision
@@yoyo12345 Thanks
@@UNknown-xk1hh ur comment was 6 months old!?! Didn't expected that u will reply
Btw what's Ur unity progress?
Thanks alot!
when i do ctrl + . the same things dont pop up and i dont have the max health and player health on my player
Come on how can this video be underrated, maybe gamedevs don't like jokes?
(If you have seen a Unity's video where two people try to joke while teaching, gets the same reaction, I feel like gamedevs don't like jokes?)
how do you make a health bar that goes down if you collide with something? I'm trying to make this game and I just cant find a way to make my player lose health if it collides with an enemy object. Plz help
I am getting a null reference for the float fillValue please help
Same problem
Where i can take PlayerHealth script?
I know this is not theme of the video but how can i make player lose health when he is colliding with something
I'm having a problem, Object Reference not set to an instance of an Object. Send help pls😢.
Update: Already fixed it, just a bug from Unity I guess, the script was applied to multiple objects dunno why.
pov: Your looking for the script
thanks. it is really helpful.
Cool, I'm glad :)
how did you create the currenthealth?
good video thanks
put the castle is still attacking you even after you died? how to stop them attack when u die?
Well in this video its hardcoded.. the short answer would be to check if your player health is equal to 0 and then stop your enemy AI from attacking the player... maybe re-enter patrol mode or something.
Thanks!
does this video has a next video? i mean how can we reduce the current health bar
float fillValue = PlayerHealt.CurrentHealth / PlayerHealth.MaxHealth;
It is great indeed,however
if my currenthealth < maxHealth slider is min value
currenthealth > maxHealth slider is max value
how can i fix this
It's probably because you are using int variables for your maxHealth and currentHealth. Integer can't store decimal values, they will automatically be rounded down. So 60/100 would equal 0 instead of 0.6. Try using float instead.
Thanks!
@@DerCriticer your solution fixed it for me, but in his tutorial he used integers and it worked. how does that make sense? I followed it exactly like he said and i got the same issue that Lyuboslav had
I'm not sure if you'll see this, but hopefully you do. But I'm having a bug where the slider only works properly for max health and nothing else, do you know how to fix this?
I figured out solution. When you have float fillValue, then delete / maxHealth and then in Unity set the max value of the slider to 10.
@@vladeko4258 I just decided to go to a different tutorial by Night Run Studio that luckily worked
It's as simple as putting (float) before the "playerHealth.currentHealth" and "playerHealth.maxHealth".
@@heinkelion thank you soooo much. In my playerHealth script I used public int instead of public float and because of it as soon as my player took damage the health bar dropped to 0
@@heinkelion i love you. thank you
where is the player health script ?
I was looking for this, too! Looks like he answers this in SneakyTurtle's comment below from 3 months ago.
using a slider made it much easier lol I wish you showed how the arrows were causing damage tho
When starting out especially its great to utilize the tools Unity provides for us! I can definitely show how this is done, the quick overview is when the arrow collides with the player it makes a call of GetComponent() and subtracts 1 from the currentHealth!
For some reason it's not letting me drag the Fill Image onto the FillStatusBar script (the script on the slider). Anyone know how to fix it?
Nevermind, I found it. Brilliant tutorial!
Sorry this took me a while to respond! Appreciate the kind words!
So I got it to work, but I will like to get it to work whenever an enemy shoots me or collides into me, it reduces my health. I was thinking to add it in this script yet I was getting an error: nullRefrenceException object reference not set to an instance of an object.
public class DestroyByContact : MonoBehaviour
{
public GameObject enemyExplosion;
public GameObject playerExplosion;
public PlayerHealth playerHealth;
public int scoreValue;
private GameController gameController;
private void Start()
{
GameObject gameControllerObject = GameObject.FindWithTag("GameController");
if (gameControllerObject != null)
{
gameController = gameControllerObject.GetComponent();
}
else
{
Debug.Log("Cannot find 'GameController' script");
}
}
private void OnTriggerEnter2D(Collider2D other)
{
// Name of the given object that collided with the enemy
var name = other.tag;
// if the name of the tag is the same as to Boundary tag
// return and do nothing
if (name == "Boundary" || name == "Enemy")
{
return;
}
if(enemyExplosion != null)
{
Instantiate(enemyExplosion, this.transform.position, this.transform.rotation);
}
if (name == "Player")
{
Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
gameController.GameOver();
}
gameController.IncrementScore(scoreValue);
Destroy(other.gameObject);
Destroy(this.gameObject);
}
}
Doesn't work.
Edit: Now works but I changed the equation using Math.Lerp to change the answer into a float
What’s the payer script
Hello, great tutorial! I'm trying to make it automatically take one away from the playerHealth when the player gets hit by an enemy, but it's not working as it's a float. Does anyone know how to get around this?
You can certainly subtract 1 from a float.
float myhealth;
myhealth -= 1f;
@@BMoDev Ohhhh, I'll try that again - thanks!
@@BMoDev No, sadly it's not working: here's the error:
error CS0019: Operator '-=' cannot be applied to operands of type 'PlayerHealth' and 'float'
and here's the code:
public int fillValue;
public PlayerHealth playerHealth;
public void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Hurt")
{
playerHealth -= 1f;
}
}
}
Ah I see, Ok
So PlayerHealth is a class, you need to access the variable inside of the class.
Not sure how your project is setup, but you can create a new script called PlayerHealth, and inside that just add two variables
public class PlayerHealth: MonoBehaviour
{
public float currentHealth;
public float maxHealth;
}
and attach it to the Player. If you click on your player, in this script you'll see these variables appear and you can set values to them.
----
then, back in your HealthBar script, use playerHealth.currentHealth -= 1;
let me know if you get it working!
@@BMoDev Just got it working - thank you so much! Incredibly helpful tutorials and you put so much work into helping others, one of the most useful game dev tutorial accounts i know. Keep it up!!
Hmm my issue is that it goes straight to 0 my health script gives the player 10 hp but it just goes straight to 1
Are you using integers instead of floats? My first guess is the integers (which won't allow decimals) is messing up the math. If you are, try using floats
@@BMoDev This worked change your current and max health to a float😌
New subscriber here! I really like your videos but when I'm watching you script I can't really see what you're typing mainly because it's to small
ayyy thanks for the support! This was something brought up so in my more recent videos I have the font zoomed in on, it should be better going forward :)
Bro can you make a video for 2020 unity but good video bro keep it up
whats the player script?
TY TY TY
U didnt show us the player health script
PlayerHealth is a script attached to my Player GameObject. It just has two float variables within it :
public float maxHealth;
public float currentHealth;
There's nothing really to it which is why I didn't show it, you'll have to create a health system for your project to work with the health bar.
@@BMoDev thank you so much
Why doesn't it show my fill image?
Not sure what your setup is. The slider by default gives you a fill image that shows.
@@BMoDev I fixed it thanks for the reply.
@@bsdev6894 how did you fix that? I think im having the same issue
@@bsdev6894 how did you fix that?
Please help me i am literally having a pain
I want it to have 3 colors tho, but it will only go green and yellow, and it will never reach the 3rd else if, pls help
void Update()
{
if (slider.value slider.minValue && !fillImage.enabled )
{
fillImage.enabled = true;
}
float fillValue = liv.CurrentHealth / liv.MaxHealth;
if (fillValue >= slider.maxValue / 1.7)
{
fillImage.color = Color.green;
}
else if (fillValue < slider.maxValue / 1.7)
{
fillImage.color = Color.yellow;
}
else if(fillValue
For yellow also check if > 2.5
@@BMoDev yea thx
gonna waste my time and now show how you made the player health script ? nike
PlayerHealth script just holds 2 floats, no functionality:
public class PlayerHealth : MonoBehavior
{
public float currentHealth = 10;
public float maxHealth = 10;
}
let me know if you need help :)
@@BMoDev I was looking for this as well; thanks!
5:55
how can we make the health bar withou the health script thanks for wasting my time
The script has 2 float variables
public class PlayerHealth : Monobehavior
{
public float currentHealth;
public float maxHealth;
}
Are you able to get it working?
BMo already fixed it but thanks
i can't add the script into the Slider cause it says the script class cannot be found
got it working
@@Lavender34124 how did you get it to work I'm getting the same errors, I fixed the script and image but the player health is still not working
whats the player script?
public class PlayerHealth : MonoBehaviour
{
public float currentHealth = 10;
public float maxHealth = 10;
}