- Home /
Lerpz Health Issue
I am still really new to programming and I am having a hard time getting this problem fixed. In the game, Lerpz starts with 4 lives. I want it so when he picks up one of the heart icons, if he is at full health, he gets an extra life. The code works when he is not at full health by adding one point to his overall health, but I can't figure out how to add more lives. I have attempted some things, but like I said, I don't know much about programming.
Do I have to add something to the Pickup script or the ThirdPersonStatus script? Also, what do I add?
The part of the Pickup script that mentions health:
function ApplyPickup (playerStatus : ThirdPersonStatus) { // A switch...case statement may seem overkill for this, but it makes adding new pickup types trivial. switch (pickupType) { case PickupType.Health: playerStatus.AddHealth(amount); break;
case PickupType.FuelCell:
playerStatus.FoundItem(amount);
break;
}
return true;
}
Part of the ThirdPersonStatus that deals with health/lives:
function AddLife (powerUp : int) { lives += powerUp; health = maxHealth; }
function AddHealth (powerUp : int) { health += powerUp;
if (health>maxHealth) // We can only show six segments in our HUD.
{
health=maxHealth;
}
}
Thanks for the help!
Answer by mrblurgle · Aug 05, 2012 at 09:59 AM
This post is getting on now, but I had a similar issue under Unity 3.5.3.x - or so I thought. My issue was that the GUI wasn't updating when I picked up fuel cells, but the cut scene was working! I eventually figured out that I had two instances of ThirdPersonStatus.js for some reason - one (correctly) on the Player object, and another (incorrectly) on the JetPack. after removing the JetPack script component, my health and fuel GUI began to update as expected. The annoying thing about this is that it didn't throw any sort of exception or error. Make sure you haven't done something similar.
As for your problem, one you have checked the above, there is a clear typo in the provided code. I have included a fixed version of the code with the bug commented out:
function AddHealth (powerUp : int)
{
health += powerUp;
if (health>maxHealth) // We can only show six segments in our HUD.
{
//health=maxHealth;
lives += 1;
}
}
In other words, why make the health maxHealth when health is already > maxHealth (also, this should really be >=)? What we actually want to increment is lives.
Your answer
![](https://koobas.hobune.stream/wayback/20220613121422im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Player lives script help 1 Answer
insert semicolon 2 Answers
How to deplete health every second 2 Answers