HOW TO MAKE A SIMPLE HEALTH BAR IN UNITY! Unity 2D Tutorial
ฝัง
- เผยแพร่เมื่อ 23 ธ.ค. 2024
- In this video I'll show you a quick and simple way to create a HEALTH BAR in Unity!
Check out my previous 1 minute tutorials: • UNITY TUTORIALS
if there are any tutorials you'd like to see, feel free to leave a comment below!
------------------------------
Follow me on TWITTER: / jakemakesgames
The music found in this video: • Video
(no copyright is intended by using this music, all rights go to their respective creators).
------------------------------
When i try to run it, it says "The type or namespace name 'image' could not be found. What can I do to fix it?
Be sure to include ‘using UnityEngine.UI;’ at the top of the script
@@JakeMakesGames make this top comment, (you forgot to mention it in the video)
@@GumbiOfficial thank you for mentioning that!
@@GumbiOfficial Minute 0:45
@@GumbiOfficial (you forgot to listen)
Best tutorials are simple and short, giving you a glimpse on how to get started. Thanks for this, other content creators would wrap this in a AAA tutorial which require you to watch it all to find the solution you are looking for.
Thank you, the main reason I did the 1 min tutorials was because I was sick of watching long videos that had a bunch over fluff in them
Straight and simple. And best part is you can use this with your own images (which I did in my case). Thanks!
glad this could help you out!
Thank you so much for the quick tutorial. I'm so surprised how easy it is to make a game.
Glad you enjoyed it!
0:15 I do not have a "Square" or a "Circle" on the source image selection part. I have the rest of them. How can I get the Square option?
You can navigate to the package manager and install the “2D” package, this should include everything; if not you can just import your own white square sprite and that should do the job!
MY BOY HAS BEEN CRUSHING THEM OUTTTTTTTTTT
THANKS BOII
Great video, thanks for help!
No worries! Glad it helped!
Hi there, @JakeMakesGames, when I do this, right after i make the canvas, these white lines show up, in the middle of the canvas, on the rendering screen. How do we prevent this?
The only thing I can think it would be is that you have gizmos turned on in the game view
@@JakeMakesGames thanks so much! that fixed my problem!
@Axy200 you’re welcome! Those gizmo lines will only appear in the engine and aren’t rendered in the final build
Worked like a charm! Thank you very much
You’re welcome! Glad it helped!
when I set it to game view the ui doesn't show up, however it does if I set the render mode to world space
Made sure on the “canvas scaler” component “ui scale mode” is set to “scale with screen size”, that may help your issue!
@@JakeMakesGames thanks you! I realized now that the canvas scales to the screen already and doesn't need to be within the camera. I thought I had to keep the images within the camera to see it in game 😓
@@RootsOf7 thats odd.. if you create them as UI images as a child of the canvas, it should be fine
When I try it, since the part of public void TakeDamage, it says 0 reference and all of the code under the void Heal and TakeDamagae has a red underline. Can you tell me how can i fix it?
It’s a bit hard to know what’s going on without seeing your code 🤔 make sure there’s no spelling errors and everything is in the correct place
Short.. Old.. BUT very very useful video! Thank you for it!
You’re welcome! Glad you enjoyed!
It's possibly that I missed something but when I click on source image to select a Sprite, I can't find square. Do I need to download it from somewhere?
You can either import your own, or if it is missing from Unity you may need to install the 2D package from the package manager
i can't find it either, u can right click in the project window, Create => 2D => Sprites => Square. Then drag the square sprite u just created into the source image spot and it should work just fine
Thank you for this tutorial but i have a question is how to use this health bar for my player take damage when being attack by enemy?
In on trigger enter (or how ever you’re handling collisions) you need to check if the enemy collided with the player and then get reference to player health script then you’ll be able to call the take damage function
Amazing tutorial saved me hours of video watching!!!!
Thanks for the kind words! I’m glad it helped!
how do i reference the takedamage to do actual damage?? from a enemy
A quick way of doing this is ensuring the health manager is a singleton; so at the top of the health manager script:
Public static HealthManager instance;
void Awake()
{
instance = this;
}
And then in the enemy script in the function you’re checking with collisions with the player
HealthManager.instance.TakeDamage(damage varable here);
Another awesome video
Yo! Thank you so much!
when I press play the Health Amount constantly increases, however, when I disabled the heal part of the script this stopped but I would still want the healing part to work... any ideas?
Check to see how the heal function is being called. If it’s being called in update without and checks it’s going to increase constantly
thanks showed me what i needed :D
Glad it helped!
Nice tutorial, but im lost how to make the damage on collision with enemy. I tried to do simple if tag player collides with tag enemy, it TakeDamage(20);. But that aint work. Do i need to make script for the enemy or add something more
For that to work, both the player and enemies need a collider of some sort and at least one of them need a rigidbody.
Nice video but you may want to include
if( healthamount < 100)
{
healthamount = 100;
}
to prevent people gaining ghost health :)
Good point, thank you for that!
For anyone trying this, make sure the smaller than sign is replaced with a larger than sign
it says The type or namespace name image could not be found
To need to add “using UnityEngine.UI” at the top of the script to get access to the UI elements in code
what do i do when it says 'Application' does not contain a definition for 'LoadedLevel'
That means the function ‘LoadedLevel’ doesn’t exist as an extension of ‘Application’. Rewatch that part of the tutorial, hope this points you in the right direction
i have stumbled upon a problem. it says that 'float' does not contain definiton for fillAmount on some lines of code in your script. i will be grateful if someone wants to help me
It’s because float doesn’t contain that. The healthbar variable that uses that line of code should be an image
When I create the "image" component and select square, my health bar doesnt show anything, like if it was invisible. When I create a sprite and try to make it bigger, only the collision box get bigger and not the sprite
make sure there is a sprite in the image component
Thanks brooo Good tutorial + new Sub
Thanks i appreciate that!
Bro went at super sonic speeds💀
That’s the point of 1 minute tutorials I guess. Quick and straight to the point
@@JakeMakesGames I like it. I can tell you know your stuff
Thank you I really appreciate that!
easy and short thank you
you’re welcome! Glad it helped
this is what i want ,thanks
You’re welcome!
You didn't explain how to make the tab that says health bar, I don't know how to get that
right click in the hierarchy, create empty, and rename it to health bar. then drag and drop the black green and red squares to it. hope i helped!
@@disqunitythank you! 🙂
@@KW-sn6hl if you were talking about the tab on the very top, then the way you can create that is by right clicking in the project folder (of the unity file explorer on the bottom) and creating a new scene with the name "Health Bar" and then open the new scene. hope this helps!
How to I put the health bar in the top left, I can't figure that out
@KaylaWellman-j5d you can anchor it to the top left if you click on the ui objects rect transform component (there will be a square crosshair looking icon) in the inspector. That’ll show you all of the anchoring options
How do i connect my health bar to my player?
I managed to do that, but my health bar goes behind my platforms, so how do I work it out?
That’s odd, if it’s a standard UI canvas it should be displaying above everything. How is your canvas set up, screen or world space?
@@JakeMakesGames It is in world space so that i can link it to my player
You’d have to look into the sorting layers
When I click the return key ONCE, the health goes down to the bottom and resets the scene. Please Help!
Make sure the input check is getkeydown and not just getkey
Thank you I enjoyed this video, I wish you would talk a litte slower and explain a little bit of the "why" on some of the steps just so we could learn more and it would be easier for non native English speakers.
Thanks for the feedback! I’ll keep that in mind for the next tutorial!
Image has no attribute fillAmount?
once you have dragged in a sprite for the “Source Image” a drop-down with the name “Image Type” will appear. Click that drop-down and select “Filled”
@@JakeMakesGames i know i found this, too, but i still cannot access it in the code then. I came to the solution to use a slider which was easier for me
That’s very strange? Glad you found a solution that works for you!
Maybe because I'm using vsc. I didn't look if it compiles. But thanks anyway, good video =)
@@JakeMakesGames fillAmount doesnt work anymore sadly. the new using directive is used now. using UnityEngine.UIElements. R.I.P using UnityEngine.UI;
i got this
Non-invocable member
I’m going to need more context
are you doing a speed run or something?
nope, just like making tutorials that are short and straight to the point without 10 minutes of fluff in the video
@@JakeMakesGames lol i had to watch it frame by frame to see what you were doing
@@BlueMoShorts did it work once you followed it?
@@JakeMakesGames it did but it was annoying you can still make it short but easier to follow
my unity keeps crasing
Not sure if it’s due to compiler errors or your machine? Are you getting any errors?
dont have the square sprite
You can either import your own or import the 2d package from the package manager
@@JakeMakesGames I got the sprite but now the health bar wont go down, it does kill the sprite but dosnt vissualy go down
@@CarloArena-b6w ensure the image
Ui components “image type” variable is set to “filled”, hope this helps
@@JakeMakesGames thx works now, best tutorials not 10 hours long
@@CarloArena-b6w glad you got it working! I appreciate it
I copied your exact code and now I have 24 errors
Please tell me what I did wrong
I can’t really help you without knowing what errors you had
@@JakeMakesGames The fact you actually replied Is astonishing to me, even tho this video was made months ago
@@GoatVibez637 I try to reply to every comment when I can! What issues are you having?
@@JakeMakesGames
There were red squiggly lines under almost every single program I wrote
@GoatVibez312 what did the errors in the console say?
thankyouuuu :)
No worries!
i really needed to put this to 0.5 speed to see whats going on do lil slower next time and btw didnt work
thanks for the feedback. Did you get any errors in the console to indicate why it wasn’t working?
banger song
🔥🔥🔥🔥
Great tutorial, however its impossible to follow along in a nice pace, always have to rewind and pause since you're speedrunning while talking lol..
Thanks for the feedback! I’ll keep that in mind when I work on a new tutorial
Just slow it down
I do get that, but the idea of the “1 minute tutorials” is for them to be quick and to the point.
@@JakeMakesGames no i think hes meaning to say "slow down the talking" as in dont talk too fast cuz its kinda hard to catch up
i PERSONALLY am NOT complaining cuz this tut was GREAT. ur REALLY underrated ngl
This is exactly what I was looking for. A quick visual representation and overlay of the UI system for health in Unity. There are many tutorials and many ways to do it, but only a few people know how to sum it up quickly to provide an effective general idea without taking 21 minutes. This is gold.
I can't find fill button can anyone help
On the “image type” drop-down on the UI Image you can click that and it’ll give you some options. Fill is there
@@JakeMakesGames there isnt a image type dropdown menu tho
Time stamp 0:13, once you give the image component a “source image” it appears as an option
@@JakeMakesGames yep got it thanks
u should put the script in the discription
It’s on my patreon :)
why you clicking so fast brother
where's the code
It’s not online anywhere, just follow along with the tutorials.. that’s kinda the whole point of them
satana
whaaat? hahah
HELP!!!!!!!!
whats the issue?
wow you talk fast ._.
dont see image type
you need to ensure your UI image has a sprite in order to see that option
@@JakeMakesGames Thanks, now i see it
@@Iiiiii9998 no worries!