#Heart system
1 messages · Page 1 of 1 (latest)
@muted citrus so whatâs the issue then
Making a thread in case other people want to use the coding channel đ
the hearts when i press the play button aint apearing
thats no problem thank you for wanting to help
Ahh perfect timing anyways
Do you have them assigned as the prefab?
Can you resend the script thatâs supposed to spawn in the hearts
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
}
And the hearts arenât being spawned in the hierarchy?
Look in the hierarchy when the game starts
Inside the for loop we just fixed put print(âhearts: â + i);
Thatâll let us know if the code is running
after the i++
where in the for loop .
under creatEmptyHeart()
its caused errors
Show it
never mind i fixed them sorry it was my dyslexia
Weird
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
Thatâs fine, it still shows that the code didnât execute
for some strange reason
print the value of heartsToMake
if it return 0, then the for loop wonât run
print heartsToMake = (int)(playerHealth.MaxHealth / 2 + MaxHealthRemainder);
like that
Make sure you didnât make it a string
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
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
so in between int and for
Yes
Yes it should be a number thatâs greater than 0
Otherwise the for loop doesnât run
ok so how would i fix that
What number is it printing?
Whatâs the value of MaxHealth?
What is the value of MaxHealth
Itâs a variable of playerHeath
max health is only mentioned twice and its on these 2 lines
Look at your player health script
And look for the value of MaxHealth
float MaxHealthRemainder = playerHealth.MaxHealth % 2;
int heartsToMake = (int)(playerHealth.MaxHealth / 2 + MaxHealthRemainder);
``` ok doing that now
{
public float health, MaxHealth;
}
i dont know how to do that
Oh actually thatâs probably why
Youâre doing 0/2
Which is 0
You attached the player health script to an object. Look at the inspector of that object
Itâs a prefab. Make sure you assigned the right script
I mean for taco
yes
taco is also a prefab
yes but not asigned to the scrips
as those are for the hearts
the health script needs the heart prefabs
Make sure your HealthManager script has a reference to the right player health script
On this image, did you drag in Taco in the Asset Folder, or Taco in the hierarchy
Thatâs what I mean
taco from the hierarchy
but the prefab has the exact same scripts
im so anoed i cant get this to work
Replace the print that printed hearts to make with:
Debug.Log($âValue of: {playerHealth.MaxHealth} divided by 2, is {heartsToMake}â);
Just copy and paste that in
Then screenshot the message for me
Replace the â with quotation marks
My phone just types them differently
ok so i need to somehow set the max health to 6
any idea how i would do that in code
will it always be 6?
laziest way to fix it:
playerHealth.MaxHealth = 6;
in void Start()
Wait screenshot healthManager for me
in the playerHealth script just remove the playerHealth.
just have MaxHealth = 6;
player health is the name of the script tho
deleeting that will ruin the script wont it
Line 11
public class playerHealth : MonoBehaviour
{
public float health, MaxHealth;
public void Start()
{
MaxHealth = 6;
}
}
yes thatâs what I mean
what would i asign the prefab to
the heart . the taco . the manager ?
wait i do have a health script . its on the heart
Screenshot line 21 of the health script
heartimage.sprite = emptyHeart;
Also
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
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)