#frontHealthBar is null / not assigned
1 messages ยท Page 1 of 1 (latest)
also the front bar works perfect, its just the back bar doesnt follow the front bar
well works perfect . you still got a null ref
and second bar doesn't work because the lerp is wrong
how should the lerp be written
if its supposed to animate in this case, it occur over a series of frames
so can i ignore it, or does it have to be fixed
you don't ignore null references no
fix it
but i dont know how, its supposed to be an image and I already dragged the image into the variable, idk how its null
is that what you mean by give it a value?
maybe you have a copy with this script somewhere where its unassigned
you would search the hierarchy with t:PlayerHealth
i only have 1 player in my hierarchy
what
oh sorry
I read wrong
yeah only 1 thing shows which is my player(the gameobject that has the script attatched)
clear the console window, re-run the game and send any new errors that show, screenshot the inspector where the script is
my player health goes down when i click a button currently, because i havent implemented enemies yet.
but as you can see my health goes down but not the backround, but when my health is 0 you can see the yellow go down a very little bit, but it should match the health bar just with a little delay
send the fullscript PlayerHealth from site !code
๐ Large Code Blocks
Use links to services like:
https://gdl.space/, https://paste.ofcode.org/, https://hatebin.com/, https://paste.myst.rs/, https://hastebin.com/
๐ Inline Code
Surround code with three backquotes. Not quotation marks.
To format as C#, add cs to the first line:
```cs
// Your code here
```
Add a comment with a line number if there is an error message.
copy paste the whole script?
is any website preferred that you want me to post on
so i can make it easier for you
any is fine from there
also can you show a screenshot of the search you wrote on the hierarchy
this is at runtime ?
are you sure you don't have 2 PlayerHealth scripts on player
the error would not throw any other way unless frontHealthBar is null
oh lol
let me test it
well at least it was an easy fix
okay the errors are gone now
but i still have a question
my backround healthbar (the yellow one) is following the red one
i know earlier you said the lerp was wrong so how should i change the lerp
are you trying to animate the bar going down?
yes
I would use a coroutine then so it can happen over span of frames
the red one goes down, then the yellow one slowly reaches where the red one is
thats how i want it to be
is there any way to just make the lerp faster instead?
can i take away "percentComplete" and put my own value?
maybe just try this
if (Input.GetKeyDown(KeyCode.A))
{
TakeDamage(Random.Range(1,10));
if(updatingUi) return;
StartCoroutine(UpdateHealthUI(0.2f)); //put some duration
}```
private IEnumerator UpdateHealthUI(float duration)
{
updatingUi = true;
float fillF = frontHealthBar.fillAmount;
float fillB= backHealthBar.fillAmount;
float hFraction = health/maxHealth;
if(fillB > hFraction)
{
frontHealthBar.fillAmount = hFraction;
float time = 0;
while(time < duration)
{
backHealthBar.fillAmount = Mathf.Lerp(fillB, hFraction, time / duration);
time += Time.deltaTime;
yield return null;
}
}
updatingUi = false
}```
should i delete this:
public void UpdateHealthUI()
{
float fillF = frontHealthBar.fillAmount;
float fillB= backHealthBar.fillAmount;
float hFraction = health/maxHealth;
if(fillB > hFraction)
{
frontHealthBar.fillAmount = hFraction;
lerpTimer += Time.deltaTime;
float percentComplete = lerpTimer / chipSpeed;
backHealthBar.fillAmount = Mathf.Lerp(fillB, hFraction, percentComplete);
}
}
replace the whole method as I have it
okay
where do I put this
in void Update?
wdym look at the code lol its exactly where it should be before
just replace the last two lines
make a boolean for updatingUi
you probably will need to fix it better after if the health goes down too quick
otherwise while its animating it wont animate again if another hit is taken
idk what line 35 is
its in the screenshot
oh
wait
sorry
public IEnumerator UpdateHealthUI(float duration)
so what now
edited
time to test it
it only works once, and the backround still doesnt go down to the red
show wats happening
oh forgot to switch it to time.
edited
switch chipspeed to time
yea keeping private is fine
it didnt change anything
also in inspector my chipspeed is slowly increasing over time
well kinda fast actually
hmm let me seee the new script you wrote
oh whoops idk how that happened
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerHealth : MonoBehaviour
{
private float health;
private float lerpTimer;
public float maxHealth;
public float chipSpeed = 2f;
public Image frontHealthBar;
public Image backHealthBar;
private bool updatingUi;
// Start is called before the first frame update
void Start()
{
health = maxHealth;
}
// Update is called once per frame
void Update()
{
health = Mathf.Clamp(health, 0, maxHealth);
if (Input.GetKeyDown(KeyCode.A))
{
TakeDamage(10);
if (updatingUi) return;
StartCoroutine(UpdateHealthUI(0.2f)); //put some duration
}
}
private IEnumerator UpdateHealthUI(float duration)
{
updatingUi = true;
float fillF = frontHealthBar.fillAmount;
float fillB = backHealthBar.fillAmount;
float hFraction = health / maxHealth;
if (fillB > hFraction)
{
frontHealthBar.fillAmount = hFraction;
float time = 0;
while (time < duration)
{
chipSpeed += Time.deltaTime;
float percentComplete = lerpTimer / time;
backHealthBar.fillAmount = Mathf.Lerp(fillB, hFraction, time / duration);
yield return null;
}
}
updatingUi = false;
}
public void TakeDamage(float damage)
{
health -= damage;
lerpTimer = 0f;
}
}
there you go
did i do something wrong?
i did, whats wrong with it?
look closely
ohhh both chipspeeds have to be changed to time correct?
i didnt notice there were 2
i thought there was only the 1 i changed
yes new one is just time+=
rather than a speed it will be a specific duration
i see that i missed it
i dont really know wat this stuff does
well some of it atleast
coroutine helps run a function over a series of frames as it were inside Update
you can adjust duration
how?
just fyi the Lerp could've also worked without coroutine if it was inside Update , it still needs to happen over a series of frames to be a smoothing animation
oh okay
also how do i change the speed of the animation?
i tried messing with chipSpeed but it did nothing
ofcourse not chipSpeed isn't used anywhere in that code now is it
ohhhhh
i put 0.2f as an example you can make a variable for it and adjust, now its based on duration instead of speed
0.2 means animation is 200 milliseconds-ish
1 is 1 second correct
yea
can i change that number to chipSpeed so i can change it in inspector
if you want
there might be issue to solve where if you will tap to fast now, the number will go down and TakeDamage but wont animate
yes i was about to say that
but that should be a problem right?
if i make it when an enemy hits me it will just go down, because its 1 input correct?
I mean yeah but its better to fix it corectly , what if you take faster damage than the animation kept up
then healthbar amount wont match health in logic
wait so if one thing damages me with 20 dmg and a different hits me with 10 dmg, it wont be the same?
depending on the style, but to solve it instead you would have to drain it to real new value rather than the old value
so basically you need to cancel out the previous coroutine and start a new one with new value
if updatingUi is true while someone else is hitting you , the health will go down but the healthbar wont update
so to solve that you could cancel out the previous coroutine animating and start a new one with the new values
because of this
if (updatingUi) return; StartCoroutine(UpdateHealthUI(0.2f)); //put some duration
so how do i cancel that out and start a new one
this is saying, if its already updating UI then return out of this function and dont run anything underneath it within this if statement
you would store the Coroutine UpdateHealthUI inside a variable
noo
so private Coroutine updateUICoroutine
if(updateUICoroutine != null) StopCoroutine(updateUICoroutine );
updateUICoroutine = StartCoroutine(UpdateHealthUI())
and you can get rid of this if (fillB > hFraction) which is probably useless here
and replace the bool in the coroutine from updatingUi = false to updateUICoroutine = null
then you can remove the bool completly
makes sense?
um yeah totally
so here im making a new void?
they're not called void
oh
void is a return type for a method / function
means returns nothing
and no, nowhere did I make a function there
oh
where do i put this
replace it where the other startcoroutine and bool was
after takedamage?
yes
i replace it with the new one correct?
yea the whole part
instead of checking bool, its checking if a coroutine is already running (in this case not null since we set to null when its done)
UpdatehealthUI is underlined in red
fixed edit
I accidentally wrote StopCoroutine(MethodName) instead of StopCoroutine(theCoroutineVariable)
if(updateUICoroutine != null) StopCoroutine(updateUICoroutine); // stop any previous coroutine that may exist
updateUICoroutine = StartCoroutine(UpdateHealthUI()); //start new coroutine and assign it to variable ```
so do i change "UpdatehealthUI" to updateUICoroutine ?
look at the example
i am but its still underlined in red
screenshot where its underlined red
yea oversight
you need there what? do you remember what goes inside UpdateHealthUI parenthesis ?
look at previous code if you must, but should be easy to figure out
it tells you in the error whats wrong
in the error message at the bottom it says "There is no argument given that corresponds to the required parameter 'duration' of 'playerHealth.UpdateHealthUI(float)
yes its telling me theres nothing that corresponds with duration i think
right?
unless im wrong
the required parameter duration
its expecting duration with the type shown there
this function wants float for duration
you did not pass float for duration
so it needs a number for the duration
ohhhh
so i can just put chipSpeed again?
YES
IT WORKSSS
even when I spam it
nice 
no worries ๐
how do you have so much time to help people?
why dont you make your own games instead and ect...
is this your job or something? to help people
nah its not my job, I just multi-task and work on my stuff , once in a while I drop in to help someone
it just fun to do because I've been there before so I know how diffcult it can be
how long have you been programming for
not very long maybe hitting 4 years
also props to you bro people like you make the world spin ๐
do you have any games I can check out?
ironically I started with Game Maker because it had drag n drop and I found coding scary ๐
nothing released yet
yeah it comes with time(repetition actually) you can do the same
they're just sitting on my harddrive just never really urged myself to post anything
maybe some jams I have on my itch, but they're mostly unfinished
were there any courses you used or anything
in the beginning I used a few videos to get me specific things I needed, not the best way to learn
well if you ever need some crappy pixel art you can dm me and ill do it for free
the Unity Learn website wasnt that good when I started
oh really?
yeah that would be cool. I'm bad at art lol
yeah since youve helped me so much, its the least I can do
what kind of game are you working on?
im trying to make a 2d platformer
its for a game jam that ends in 24 hours.... soooo theres that, but Im going to keep working on it even when the game jam is over
does this link work for you? https://trello.com/b/sfuBZ5R1/first-game-jam
Organize anything, together. Trello is a collaboration tool that organizes your projects into boards. In one glance, know what's being worked on, who's working on what, and where something is in a process.
those are all my ideas for the game
doesn't work says its private
oh okay let me make it public
Organize anything, together. Trello is a collaboration tool that organizes your projects into boards. In one glance, know what's being worked on, who's working on what, and where something is in a process.
there you go just please dont change around my stuff
okay
https://www.youtube.com/@navarone77
here maybe you can drop me a follow . I need to update everything soon, but anything counts lol
subbed and have notifications on so ill see once you post
looks good, you can def knock the first 3
attac/currency also, maybe the UI stuff might be time consuming
sorry can you rephrase that the comma is throwing me off
ohhh
nevermind
had a brain fart
i see what your saying
youre saying i can probably knock out currency system and attack, but the ui stuff will take long
in the ToDo
maybe info card might be time consuming, would def try that last
enemy if you don't have it will take most of the time
yeah that will be the last thing I do anyways because i will add more stuff and wouldnt really make sense to start now
enemy will take the most time?
why is that
i thought that would be easy
I mean depends, AI can range from very simple to very complex depending on the game
does your enemy just go left and right or does it jump between plaforms etc.
i just want simple enemies that try to attack me once im in a certain distance
oh my gosh i didnt think of that
yeah they just walk back and forth
yup
they dont even go for the player right? ๐ญ
some doo in a very basic fasion yea
what like the turtle or something?
think like the Football player in the old school super mario (im really old lol)
your not that old i know what the old marios are ๐ญ
i loved the old marios when i was little
id had the new mario on my wii as a kid and my cousin had the old marios
i forgot how they are named but he had the 2nd one and the 3rd one
the 3rd is my favorite out of the 3
this is the one im thinking of
yup thats def one my top ones
super mario galaxy 1 is my favourite mario game of all time though
yeah the 3D ones were trippy
sunshine was pretty good
what was your first console?
never played it but i know what your talking about
gamecube is awsome
my cousin recently just bought a modded one
it has a ton of games on it and stuff
its super cool
Only liked it for the nintendo stuff, i liked ps2 / dreamcast better
Dreamcast and ps2 games were peak gaming
im a little younger so i think ps3 games are better
like skate 2 and 3, minecraft ps3 edition stuff like that
hehe yeah you guys had the nice graphics, still very good games but nothing compares. Maybe its nolstagia lenses haha
nolstagia will always blind you
okay i forgot about ratchet and clank
i played that on psp
you literally just unlocked that memory
i forgot i had a psp
haha yeah i might spin up my old psp
i want to animate the blue plart of my health bar btw to shrink as i use and abilty
like shrink inwards towards the middle
which bluepart ?
oh thats not also an Image set to fill?
no
which part / how do you want to shrink it
im confused on that, cause if you wanted to slide that you can change fill amount to move from up/down
or you mean shrink as in ball gets smaller in size?
i want the blue part to shrink into itself
yes
oh then yea use the scale
okay
i want it to scale down slowly as i use an abilty (not sure of the name yet) and once its at 0 i cant charge the ability anymore
then i need the blue to come back so i can reuse it
same concept as fill slider
but apply it to the scale
so just a coroutine for animating it
so i can basically just copy paste the code and change some things?
then instead you can do Vector3.Lerp
if its UI you probably want to scale sizeDelta of rect
then you can use Vector2.Lerp
delete the variable, its unused as it says
but then it messes with the code
oh wait
i lied
i didnt need it
i asked chat gpt to make this, would this work?
https://gdl.space/eporosinav.cs
idk why is keeps doing that
you wont learn much from generating random code, also just fyi we're not really supposed to help with "AI" code .
oh okay thank you
and yes i know its not a good way to learn
i just want to get as much progress before the game jam ends then i will take learning more seriously
yeah but now it made this nonsense code
since there is only 22 hours left anymways
why use fill amount when goal was supposed to be changing scale inwards
this is what i told chat gpt:
write me a code for unity that makes a circle in my UI shrink to nothing while im using an abilty, and when the circle shrinks to 0 i cant use the ability anymore and the circle slowly comes back
its just bad cause it doesnt think or have any real context to the question, it just grabs whatever closest match to those words and code with it
its not actually creating any logic or anything
yeah
i read the code a little (i cant understand that much) and was questioning why it would fill instead of scale
like I said earlier, the solution was just to clone the same thing you did with coroutine and fill Lerp for healthbar
instead of Lerp the fill amount, you lerp the sizeDelta of the RectTransform component (you need a reference)
i totally understand if not but would you ever want to make a game together in the future, and you could do all the coding and i could do the art and music?
maybe for a game jam you enter or something
or if you ever just need a simple pixel art thing too you can dm me and ill make it for you
If I have time yeah maybe a jam sure
okay yeah just DM me and lmk
do you know how to make gameboy style art?
like all green and stuff?
send like a reference picture so i know what you mean
oh you mean like 16 bit and 32 bit ect.. ??
yeah but with like different shades and stuff right?
ohhh
you mean literally 1 color
because its limited to 1 bit
um no i do not know how to do that
i could probably do it if i could shade the colors though kinda like this
oh sure this one is also harder probably cause of different colors
they had a jam for 1 bit pixel though so just curious if you did that
i mean i can still try though if you want
it would be good practice and you dont have to use my stuff
you can just like give me a request to make something and if you like it then you can use it
sure, are you using your own sprites in your jam ?
thats good. Do you animate as well?
i havent done it yet, but i mean how hard can it be
its just redrawing the chracter each frame
if you want i can try to animate my current character and show you how it looks when its done
ill just make an idle animation
no worries do what you need to do for the jam ๐
im gonna make the idle animation anyways because im pretty burnt out from all this code stuff for now