Hi I’m new to C# so forgive me such a basic question.
I made a health bar. Wich consists of small nice red bars inside (not a plain color) so I can’t scale them in a normal way - this would look ugly.
Therefore I made another element in the bar - a black rectangle which would cover the red bars with the health loss.
The idea is that when the health drops down the black rectangle will stretch from the right side of the health bar covering more and more of the health bars.
I can’t do that by a slider delivered by unity engine because it would work the opposite way - the black rectangle would shrink from right to left with the health loss - actually UNcovering red health bars.
So i imagine I could manipulate the left edge of Rect Tranform of my black rectangle - and make it moove to the left with health loss - thus covering the health bars beneath.
But how to do it in the code?
For a refference here is my health bar when the black rectangle is assigned to the slider:
The worst POSSIBLE thing you could do is begin imagining how an API works! While I suppose you could manipulate a RectTransform, there are MANY ways that are better, such as manipulating the .fillAmount on a properly-configured Image.
The Unity API is very well documented. Look for the namespace UnityEngine.UI
Not only that but there are thousands of tutorials to teach you everything you want about the UI.
Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:
How to do tutorials properly, two (2) simple steps to success:
Tutorials are a GREAT idea. Tutorials should be used this way:
Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That’s how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.
Fortunately this is the easiest part to get right: Be a robot. Don’t make any mistakes.
BE PERFECT IN EVERYTHING YOU DO HERE!!
If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.
Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.
Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.
Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there’s an error, you will NEVER be the first guy to find it.
Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!
I wont search thousands of tutorials to solve such a specific problem.
Im now watching a tutorial on how to create a health bar. And following it.
Your answer is to generic.
If I knew how to serch for solution in a documentation I would do that.
There’s rarely ever a specific problem that can’t be solved with a good fundamental understanding. Tutorials are one such way to grow that understanding.
The answer to your problem is pretty simple, you just need to scale your health bar rect with a pretty typical formula of currentHealth / maximumHealth to get a percentage out of 100. With that in mind, it’ll just be some digging around in the API documentation to figure out how to do that.
Excellent! As long as you follow the Step 1 and 2 you should be swimming in health bars by nightfall.
GENERIC! AAhahahahaha you are HILARIOUS my friend. Do you realize that some type of “how to XXX?” question is posted at least 10 if not 20 times PER DAY here in this forum?
Of COURSE what I wrote is generic! It’s a generic question, it gets a generic copy-pasted answer!
Why would that be surprising?
Unity should automate the new-thread system so that if anyone asks a question starting with “how to…” or “how can I…” then it just redirects them to Google / Youtube search with their post crammed in the search field.
So you came here to teach me about my behavior and waste your precious time of your life to type such a helpful info like: serach in the documentation or watch you tube tutorials?
Thank you. I’m really gratefull.
What’s more, spiney199, you even didn’t make an effort to nderstand what my problem is.
It’s not a typical health bar. The scaling function will shrink the sprite - that means that nice little red bars will get thinner and thinner. What it should do instead is to “erase” portions of that sprite acording to the health loss.
So. As I see the forum is crowded with such helpful personalities.
I’ll go somewhere else.
You have quite the chip on your shoulder, mate. The answer @spiney199 gave is correct, set the fillAmount on your image to the percentage value given from the equation they gave you.
Reading the documentation and taking away what you need from it is an invalueable skill that you should learn ASAP. Learning programming without learning proper search techniques is pretty useless.
Calm down. People spend their valueable time here TRYING to help. This does not always work. And this is not always fault of the helping people! Some new users come here with quite some expectations and ego. It’s better they learn how things work from the beginning. If an answer is not what you expect you can still state that in a polite manner.
And I expect ANY forum/community you join will react similarily to such a behavior. So maybe the “fault” is on your side. Leaving only burned ground will only get you so far. So maybe you should work a bit on your character skills too?
When reading such posts I DEEPLY regret that the forum software does not allow comments on users, blacklisting (beside completely blocking), downvoting etc… This would presumably keep “manners” a bit higher.
Anyway, I’ll try to be helpful nevertheless. Tell me how it worked out for you.
Try setting the anchor to the right side and manipulating the width of the RectTransform. When health is full width is 0. When health is 0 width is whatever your healthbars width is.
Then maybe the mask component is what you need? So you would have a full healthbar and set the width of the mask as you need it. It would only show the part “below” it.
Then maybe the [mask component](https://learn.unity.com/tutorial/ui-masking#6032b6fdedbc2a06c234cd3e) is what you need? So you would have a full healthbar and set the width of the mask as you need it. It would only show the part "below" it.
Sorry for the delay in my answer and exigous - thank you for help.
That is exactly what I’m trying to do in the code.
But what the syntax will look like?
Here is my health bar and how the black mask looks like:
So. I don’t understand the way of calling parameters shown in the inspector and changing their values in the code.
If you would please be so kind and give me an example how to do it here?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mask : MonoBehaviour
{
private RectTransform trans;
void Start()
{
trans = GetComponent<RectTransform>(); // we need to get the rect transform component and store it in a field for later access
if (trans == null)
{
Debug.LogError("No RectTransform found!"); // the game object has no rect transform component attached
}
}
void Update()
{
float width = trans.rect.width; // read the current width value
if (width > 30.0f)
{
width -= Time.deltaTime * 50f; // subtract something (50 is for speed, otherwise it would shrink 1 pixel per second, hard to see)
}
trans.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width ); // apply the new width, note: you cannot apply it directly by assigning a rect because there is much stuff happening under the hood (anchors etc.)
}
}
I had to look that up. Seems to be there is no “easy” way to change width via code. Shame on you Unity!
Hope that helps.