#Heart system

1 messages · Page 1 of 1 (latest)

dusk drift
#

@muted citrus so what’s the issue then

#

Making a thread in case other people want to use the coding channel 😅

muted citrus
#

the hearts when i press the play button aint apearing

#

thats no problem thank you for wanting to help

dusk drift
dusk drift
muted citrus
#

yes

dusk drift
#

Can you resend the script that’s supposed to spawn in the hearts

muted citrus
#

sure there is 2

#

wrong button

#

sorry

#
public class HealthManager : MonoBehaviour
{
    public Health heartPrefab;
    public playerHealth playerHealth;
    List<Health> hearts = new List<Health>();

    public void Start()
    {
        drawHearts();
    }

    public void drawHearts ()
    {
        clearHearts();

        float MaxHealthRemainder = playerHealth.MaxHealth % 2;
       
        int heartsToMake = (int)(playerHealth.MaxHealth / 2 + MaxHealthRemainder);
     
        for (int i = 0; i < heartsToMake; i++)
        {
            createEmptyHeart();
        }

    }

    public void createEmptyHeart()
    {
        Health newHeart = Instantiate(heartPrefab);
        newHeart.transform.SetParent(transform);
        

        Health heartcomponant = newHeart.GetComponent<Health>();
        heartcomponant.setHeartImage(heartStatus.Empty);
        hearts.Add(heartcomponant);

        
    }


    public void clearHearts()
    {
        foreach(Transform t in transform)
        {
            Destroy(t.gameObject);
        }
        hearts = new List<Health>();
    }

}

#
public class Health : MonoBehaviour
{
    public Sprite fullHeart, emptyHeart;
    Image heartimage;

    public void Awake()
    {
        heartimage = GetComponent<Image>();
    }

    public void setHeartImage(heartStatus status)
    {
        switch (status)
        {
            case heartStatus.Empty:
                heartimage.sprite = emptyHeart;
                break;

            case heartStatus.full:
                heartimage.sprite = fullHeart;
                break;
        }
    }
   

}

public enum heartStatus
{
    Empty = 0,
    full = 1
}

dusk drift
#

And the hearts aren’t being spawned in the hierarchy?

#

Look in the hierarchy when the game starts

muted citrus
#

nope

#

nothing in the hierarchy

dusk drift
#

That’ll let us know if the code is running

muted citrus
#

after the i++

dusk drift
#

With the create new heart method

muted citrus
#

where in the for loop .

dusk drift
muted citrus
#

its caused errors

dusk drift
#

Show it

muted citrus
#

never mind i fixed them sorry it was my dyslexia

dusk drift
#

That’s fine

#

Do the messages print?

muted citrus
dusk drift
#

Weird

dusk drift
#

In the script

muted citrus
#
  public void drawHearts ()
    {
        clearHearts();

        float MaxHealthRemainder = playerHealth.MaxHealth % 2;
       
        int heartsToMake = (int)(playerHealth.MaxHealth / 2 + MaxHealthRemainder);
     
        for (int i = 0; i < heartsToMake; i++)
        {
            createEmptyHeart();
            print("hearts"  );
        }

    }

#

when i put the +i it came u with red under print so i left that bit out

dusk drift
#

That’s fine, it still shows that the code didn’t execute

muted citrus
#

for some strange reason

dusk drift
#

if it return 0, then the for loop won’t run

muted citrus
#

print heartsToMake = (int)(playerHealth.MaxHealth / 2 + MaxHealthRemainder);

#

like that

dusk drift
#

no

#

print(heartsToMake);

#

Just add that before the for loop

muted citrus
dusk drift
muted citrus
#
public void drawHearts ()
    {
        print("heartsToMake");
        clearHearts();

        float MaxHealthRemainder = playerHealth.MaxHealth % 2;
       
        int heartsToMake = (int)(playerHealth.MaxHealth / 2 + MaxHealthRemainder);
     
        for (int i = 0; i < heartsToMake; i++)
        {
            createEmptyHeart();
            print("hearts" );
        }
#

how do i check that

dusk drift
#

put print(heartsToMake); before the for loop but after the math calculation

#

Don’t wrap it in “quotations” otherwise it’ll be a string literal

muted citrus
#

so in between int and for

dusk drift
#

Yes

muted citrus
#

coming back as 0

#

so im asuming thats the issue it shouldnt be coming back as 0

dusk drift
#

Otherwise the for loop doesn’t run

muted citrus
#

ok so how would i fix that

dusk drift
muted citrus
#

0

#

exactly 0

dusk drift
#

What’s the value of MaxHealth?

muted citrus
#

playerHealth.MaxHealth % 2;

#

i think

dusk drift
#

It’s a variable of playerHeath

muted citrus
#

max health is only mentioned twice and its on these 2 lines

dusk drift
#

And look for the value of MaxHealth

muted citrus
#
   float MaxHealthRemainder = playerHealth.MaxHealth % 2;
       
        int heartsToMake = (int)(playerHealth.MaxHealth / 2 + MaxHealthRemainder);
``` ok doing that now
#

{
public float health, MaxHealth;
}

dusk drift
#

Look in the inspector then

#

Of the script

muted citrus
#

i dont know how to do that

dusk drift
dusk drift
#

Which is 0

muted citrus
#

should i put like max health =

#

then what i want the max health to =

dusk drift
muted citrus
dusk drift
#

hmm max health is 6

#

Actually wait

dusk drift
# muted citrus

It’s a prefab. Make sure you assigned the right script

muted citrus
#

i only have 2 heart prefabs

#

heart and black heart

dusk drift
#

I mean for taco

muted citrus
#

yes

dusk drift
#

taco is also a prefab

muted citrus
#

yes but not asigned to the scrips

#

as those are for the hearts

#

the health script needs the heart prefabs

dusk drift
#

Make sure your HealthManager script has a reference to the right player health script

muted citrus
#

it only lets me select one script

#

so they have to be right ?

dusk drift
# muted citrus

On this image, did you drag in Taco in the Asset Folder, or Taco in the hierarchy

#

That’s what I mean

muted citrus
#

taco from the hierarchy

#

but the prefab has the exact same scripts

#

im so anoed i cant get this to work

dusk drift
#

Debug.Log($”Value of: {playerHealth.MaxHealth} divided by 2, is {heartsToMake}”);

#

Just copy and paste that in

#

Then screenshot the message for me

muted citrus
#

loads of red underlines

dusk drift
#

My phone just types them differently

muted citrus
#

it says 0 divided by 2 is 0

dusk drift
#

Well playerHealth.MaxHealth is somehow 0

#

¯_(ツ)_/¯

#

Idk how you assigned it

muted citrus
#

ok so i need to somehow set the max health to 6

#

any idea how i would do that in code

dusk drift
#

will it always be 6?

muted citrus
#

i trives public float maxhealth but that didnt work

#

and yes the max is always 6

dusk drift
#

laziest way to fix it:
playerHealth.MaxHealth = 6;
in void Start()

muted citrus
#

its underlined red

dusk drift
#

Wait screenshot healthManager for me

muted citrus
dusk drift
#

just have MaxHealth = 6;

muted citrus
#

player health is the name of the script tho

#

deleeting that will ruin the script wont it

dusk drift
#

Line 11

muted citrus
#

public class playerHealth : MonoBehaviour
{
public float health, MaxHealth;

public void Start()
{
    MaxHealth = 6;
}

}

dusk drift
#

yes that’s what I mean

muted citrus
#

ahh ok

#

well its still not working

dusk drift
#

Is the log still showing 0 divided by 2 is 0?

#

And you saved the scripts right?

muted citrus
#

no 6 devided by 2 is 3

#

but still no hearts

dusk drift
#

You don’t have a health prefab

#

Assign the health prefab

muted citrus
#

what would i asign the prefab to

#

the heart . the taco . the manager ?

#

wait i do have a health script . its on the heart

dusk drift
#

Screenshot line 21 of the health script

muted citrus
#

heartimage.sprite = emptyHeart;

dusk drift
muted citrus
#

yes i fixed that

dusk drift
#

One of your objects with a health script has an unassigned reference

#

Sometimes double clicking it will lead you to the culprit or it’ll open the script so try double clicking it and see where it takes you

muted citrus
#

it just took me to the script

#

idk honestly

#

its saying i have unasigned things when i dont

#

only thing i can think to do is deleet the scripts from the items then re add them

#

i un added them and got no error re added them and re asigned eveeerything and the error came back

#

NullReferenceException: Object reference not set to an instance of an object
Health.setHeartImage (heartStatus status) (at Assets/Scripts/Health.cs:21)
HealthManager.createEmptyHeart () (at Assets/Scripts/HealthManager.cs:41)
HealthManager.drawHearts () (at Assets/Scripts/HealthManager.cs:28)
HealthManager.Start () (at Assets/Scripts/HealthManager.cs:13)