#frontHealthBar is null / not assigned

1 messages ยท Page 1 of 1 (latest)

acoustic matrix
#

also the front bar works perfect, its just the back bar doesnt follow the front bar

near gazelle
#

well works perfect . you still got a null ref

#

and second bar doesn't work because the lerp is wrong

acoustic matrix
#

how should the lerp be written

near gazelle
#

if its supposed to animate in this case, it occur over a series of frames

acoustic matrix
near gazelle
#

fix it

acoustic matrix
#

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?

near gazelle
#

you would search the hierarchy with t:PlayerHealth

acoustic matrix
#

i only have 1 player in my hierarchy

near gazelle
#

what

acoustic matrix
#

oh sorry

#

I read wrong

#

yeah only 1 thing shows which is my player(the gameobject that has the script attatched)

near gazelle
acoustic matrix
#

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

near gazelle
pale larkBOT
acoustic matrix
#

copy paste the whole script?

near gazelle
#

yes but on one of the sites

#

save and send link

acoustic matrix
#

is any website preferred that you want me to post on

#

so i can make it easier for you

near gazelle
#

any is fine from there

acoustic matrix
#

okay

near gazelle
acoustic matrix
#

there you go

#

is that what you meant?

near gazelle
acoustic matrix
#

while the game is running?

#

is that what you mean

near gazelle
#

are you sure you don't have 2 PlayerHealth scripts on player

acoustic matrix
#

oh my god

#

im so sorry

near gazelle
#

the error would not throw any other way unless frontHealthBar is null

acoustic matrix
#

i did

#

im so sorry

near gazelle
#

oh lol

acoustic matrix
#

let me test it

near gazelle
#

well at least it was an easy fix

acoustic matrix
#

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

near gazelle
#

lerpTimer is very small and u only press it once

#

so its decreasing very slow

acoustic matrix
#

how do I make it faster

#

increase chipSpeed?

near gazelle
#

are you trying to animate the bar going down?

acoustic matrix
#

yes

near gazelle
#

I would use a coroutine then so it can happen over span of frames

acoustic matrix
#

the red one goes down, then the yellow one slowly reaches where the red one is

#

thats how i want it to be

acoustic matrix
#

can i take away "percentComplete" and put my own value?

near gazelle
#

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
    }```
acoustic matrix
#

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);
    }
}
near gazelle
#

replace the whole method as I have it

acoustic matrix
#

okay

acoustic matrix
#

in void Update?

near gazelle
#

wdym look at the code lol its exactly where it should be before

#

just replace the last two lines

acoustic matrix
#

oh i see

#

sorry

#

those errors pop up

near gazelle
#

make a boolean for updatingUi

acoustic matrix
#

do I make a bool variable that says updatingUi

#

okay

#

private?

near gazelle
#

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

acoustic matrix
#

and there is one more error

#

its the error on line 35

#

its still the same error

near gazelle
#

idk what line 35 is

acoustic matrix
#

oh

#

wait

#

sorry

#

public IEnumerator UpdateHealthUI(float duration)

near gazelle
#

ah yes I forgot one very important part

#

it wouldve made infinite loop

#

ops

acoustic matrix
#

so what now

near gazelle
acoustic matrix
#

time to test it

#

it only works once, and the backround still doesnt go down to the red

near gazelle
#

oh forgot to switch it to time.

#

edited

#

switch chipspeed to time

acoustic matrix
#

okay

#

in private IEnumerator UpdateHealthUI(float duration)?

near gazelle
#

yea keeping private is fine

acoustic matrix
#

it didnt change anything

#

also in inspector my chipspeed is slowly increasing over time

#

well kinda fast actually

near gazelle
#

hmm let me seee the new script you wrote

acoustic matrix
#

sorry i overpassed the discord character limit.

near gazelle
#

you sent the wrong script

#

send PlayerHealth

acoustic matrix
#

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?

near gazelle
#

yes

#

did you not look a the updated UpdateHealthUI

acoustic matrix
#

i did, whats wrong with it?

near gazelle
#

look closely

acoustic matrix
#

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

near gazelle
#

yes new one is just time+=

acoustic matrix
#

ahh i see

#

well

near gazelle
#

rather than a speed it will be a specific duration

acoustic matrix
#

i see that i missed it

#

i dont really know wat this stuff does

#

well some of it atleast

near gazelle
#

coroutine helps run a function over a series of frames as it were inside Update

acoustic matrix
#

okay it works

#

the yellow does change a little too fast though

near gazelle
#

you can adjust duration

acoustic matrix
#

how?

near gazelle
#

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

acoustic matrix
#

oh okay

#

also how do i change the speed of the animation?

#

i tried messing with chipSpeed but it did nothing

near gazelle
#

ofcourse not chipSpeed isn't used anywhere in that code now is it

acoustic matrix
#

ohhhhh

near gazelle
#

i put 0.2f as an example you can make a variable for it and adjust, now its based on duration instead of speed

acoustic matrix
#

so i can change the float number to my liking?

#

oh okay

near gazelle
#

0.2 means animation is 200 milliseconds-ish

acoustic matrix
#

1 is 1 second correct

near gazelle
acoustic matrix
#

can i change that number to chipSpeed so i can change it in inspector

near gazelle
#

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

acoustic matrix
#

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?

near gazelle
#

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

acoustic matrix
#

wait so if one thing damages me with 20 dmg and a different hits me with 10 dmg, it wont be the same?

near gazelle
#

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

near gazelle
#

so to solve that you could cancel out the previous coroutine animating and start a new one with the new values

acoustic matrix
#

why is that

#

oh okay

near gazelle
acoustic matrix
#

so how do i cancel that out and start a new one

near gazelle
#

this is saying, if its already updating UI then return out of this function and dont run anything underneath it within this if statement

near gazelle
acoustic matrix
#

so do i make an else statement after?

#

oh

near gazelle
#

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?

acoustic matrix
#

um yeah totally

acoustic matrix
near gazelle
#

they're not called void

acoustic matrix
#

oh

near gazelle
#

void is a return type for a method / function

#

means returns nothing

#

and no, nowhere did I make a function there

acoustic matrix
#

oh

near gazelle
#

thats a variable / field

#

defined methods always need () and { }

near gazelle
acoustic matrix
#

after takedamage?

near gazelle
#

yes

acoustic matrix
#

i replace it with the new one correct?

near gazelle
#

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)

acoustic matrix
#

UpdatehealthUI is underlined in red

near gazelle
#

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 ```
acoustic matrix
#

so do i change "UpdatehealthUI" to updateUICoroutine ?

near gazelle
acoustic matrix
#

i am but its still underlined in red

near gazelle
#

screenshot where its underlined red

acoustic matrix
near gazelle
#

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

acoustic matrix
#

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)

near gazelle
#

correct. lets break down the error

#

do you know what method parameters are?

acoustic matrix
#

yes its telling me theres nothing that corresponds with duration i think

#

right?

#

unless im wrong

near gazelle
#

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

acoustic matrix
#

so it needs a number for the duration

near gazelle
#

yes that would be duration a float number

#

didn't you have a variable there already

acoustic matrix
#

do i put any number?

#

for duration?

near gazelle
acoustic matrix
#

ohhhh

#

so i can just put chipSpeed again?

#

YES

#

IT WORKSSS

#

even when I spam it

near gazelle
#

nice UnityChanThumbsUp

acoustic matrix
#

dude youre such a lifesaver

#

im so sorry that took so long

near gazelle
#

no worries ๐Ÿ™‚

acoustic matrix
#

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

near gazelle
#

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

acoustic matrix
#

how long have you been programming for

near gazelle
#

not very long maybe hitting 4 years

acoustic matrix
#

also props to you bro people like you make the world spin ๐Ÿ™

#

do you have any games I can check out?

near gazelle
#

ironically I started with Game Maker because it had drag n drop and I found coding scary ๐Ÿ˜…

acoustic matrix
#

look at you now

#

dropping codes like its nothing

near gazelle
acoustic matrix
#

NOTHING

#

at all?

near gazelle
#

yeah it comes with time(repetition actually) you can do the same

acoustic matrix
#

not even a crappy game from when you first started?

#

what did you do to learn?

near gazelle
#

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

acoustic matrix
#

were there any courses you used or anything

near gazelle
#

in the beginning I used a few videos to get me specific things I needed, not the best way to learn

acoustic matrix
#

well if you ever need some crappy pixel art you can dm me and ill do it for free

near gazelle
#

the Unity Learn website wasnt that good when I started

acoustic matrix
near gazelle
acoustic matrix
#

yeah since youve helped me so much, its the least I can do

near gazelle
#

what kind of game are you working on?

acoustic matrix
#

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

near gazelle
#

haha yeah thats what happens to me, all my itch.io are unifished jams

acoustic matrix
#

does this link work for you? https://trello.com/b/sfuBZ5R1/first-game-jam

#

those are all my ideas for the game

near gazelle
#

doesn't work says its private

acoustic matrix
#

oh okay let me make it public

#

there you go just please dont change around my stuff

near gazelle
#

i dont even have an account ๐Ÿ˜ฎ

#

maybe you can screenshot it or something

acoustic matrix
#

okay

near gazelle
#

https://www.youtube.com/@navarone77
here maybe you can drop me a follow . I need to update everything soon, but anything counts lol

acoustic matrix
acoustic matrix
near gazelle
#

attac/currency also, maybe the UI stuff might be time consuming

acoustic matrix
#

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

near gazelle
#

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

acoustic matrix
#

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

near gazelle
#

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.

acoustic matrix
#

i just want simple enemies that try to attack me once im in a certain distance

acoustic matrix
near gazelle
#

yeah platformers

#

the old school"mario style "ones are the easiest tho

acoustic matrix
#

yeah they just walk back and forth

near gazelle
#

yup

acoustic matrix
#

they dont even go for the player right? ๐Ÿ˜ญ

near gazelle
#

some doo in a very basic fasion yea

acoustic matrix
#

what like the turtle or something?

near gazelle
#

think like the Football player in the old school super mario (im really old lol)

acoustic matrix
#

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

near gazelle
acoustic matrix
#

this is the one im thinking of

near gazelle
#

yup thats def one my top ones

acoustic matrix
#

super mario galaxy 1 is my favourite mario game of all time though

near gazelle
#

yeah the 3D ones were trippy

acoustic matrix
#

that was the first game i ever got for my wii

#

i was like 3 or 4

near gazelle
#

sunshine was pretty good

acoustic matrix
#

what was your first console?

acoustic matrix
near gazelle
#

PS1

#

it was gamecube

acoustic matrix
#

gamecube is awsome

#

my cousin recently just bought a modded one

#

it has a ton of games on it and stuff

#

its super cool

near gazelle
#

Only liked it for the nintendo stuff, i liked ps2 / dreamcast better

#

Dreamcast and ps2 games were peak gaming

acoustic matrix
#

im a little younger so i think ps3 games are better

#

like skate 2 and 3, minecraft ps3 edition stuff like that

near gazelle
#

hehe yeah you guys had the nice graphics, still very good games but nothing compares. Maybe its nolstagia lenses haha

acoustic matrix
#

nolstagia will always blind you

near gazelle
#

sly cooper and ratchet n clank on ps2

#

so good

acoustic matrix
#

okay i forgot about ratchet and clank

#

i played that on psp

#

you literally just unlocked that memory

#

i forgot i had a psp

near gazelle
#

haha yeah i might spin up my old psp

acoustic matrix
#

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

near gazelle
#

which bluepart ?

acoustic matrix
#

so i just do that by using localScale right?

#

the blue circle

near gazelle
#

oh thats not also an Image set to fill?

acoustic matrix
#

no

near gazelle
#

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?

acoustic matrix
#

i want the blue part to shrink into itself

near gazelle
#

oh then yea use the scale

acoustic matrix
#

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

near gazelle
#

same concept as fill slider

#

but apply it to the scale

#

so just a coroutine for animating it

acoustic matrix
#

so i can basically just copy paste the code and change some things?

near gazelle
#

then instead you can do Vector3.Lerp

#

if its UI you probably want to scale sizeDelta of rect

#

then you can use Vector2.Lerp

acoustic matrix
#

also how can I make that message go away

near gazelle
acoustic matrix
#

but then it messes with the code

#

oh wait

#

i lied

#

i didnt need it

near gazelle
#

this is the playermovement script

#

also you should not use GPT

acoustic matrix
near gazelle
#

you wont learn much from generating random code, also just fyi we're not really supposed to help with "AI" code .

acoustic matrix
acoustic matrix
#

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

near gazelle
#

yeah but now it made this nonsense code

acoustic matrix
#

since there is only 22 hours left anymways

near gazelle
#

why use fill amount when goal was supposed to be changing scale inwards

acoustic matrix
#

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

near gazelle
#

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

acoustic matrix
#

yeah

#

i read the code a little (i cant understand that much) and was questioning why it would fill instead of scale

near gazelle
#

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)

acoustic matrix
#

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

near gazelle
#

If I have time yeah maybe a jam sure

acoustic matrix
#

okay yeah just DM me and lmk

near gazelle
#

do you know how to make gameboy style art?

acoustic matrix
#

like all green and stuff?

near gazelle
#

just pixel art that size

#

or 1 bit art ?

acoustic matrix
#

send like a reference picture so i know what you mean

#

oh you mean like 16 bit and 32 bit ect.. ??

near gazelle
#

bit just the limts of color basically

#

1 bit would be 1 color

acoustic matrix
#

yeah but with like different shades and stuff right?

#

ohhh

#

you mean literally 1 color

near gazelle
#

because its limited to 1 bit

acoustic matrix
#

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

near gazelle
#

they had a jam for 1 bit pixel though so just curious if you did that

acoustic matrix
#

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

near gazelle
#

sure, are you using your own sprites in your jam ?

acoustic matrix
#

yes

#

i made the healthbar and player

near gazelle
#

thats good. Do you animate as well?

acoustic matrix
#

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

near gazelle
#

no worries do what you need to do for the jam ๐Ÿ˜›

acoustic matrix
#

im gonna make the idle animation anyways because im pretty burnt out from all this code stuff for now