- Home /
health script do not work when collision detected
I wanted to create a health system for my first game it works like this when collision detected and keyboard button pressed the heart is deactivated I don't know what's the problem here's my code . #pragma strict var currentHealth : int; var maximumHealth : int = 3 ;
var heart1:GameObject;
var heart2:GameObject;
var heart3:GameObject;
function Start()
{
currentHealth = maximumHealth ;
}
function Update()
{
if(currentHealth<0)
currentHealth = 0;
if(currentHealth>maximumHealth)
currentHealth = maximumHealth;
if(currentHealth == 3)
{
heart1.SetActive(true);
heart2.SetActive(true);
heart3.SetActive(true);
}
if(currentHealth == 2)
{
heart1.SetActive(true);
heart2.SetActive(true);
heart3.SetActive(false);
}
if(currentHealth == 1)
{
heart1.SetActive(true);
heart2.SetActive(false);
heart3.SetActive(false);
}
if(currentHealth == 0)
{
heart1.SetActive(false);
heart2.SetActive(false);
heart3.SetActive(false);
GameOver();
}
}
function GameOver()
{
Debug.Log ("collsion detected");
Application.LoadLevel("play 2);
}
function OnTriggerStay2D(autre : Collider2D ){
Debug.Log ("collsion detected");
if (autre.gameObject.tag=="nine") {
Debug.Log("planet detected");
if (Input.GetKeyDown(KeyCode.Space)) {
currentHealth = currentHealth - 1;
}}}
This probably won't fix your problem, but you're code was in dire need of being refactored, so here:
#pragma strict
var currentHealth : int;
var maximumHealth : int = 3 ;
var hearts:GameObject[];
function Start() { currentHealth = maximumHealth; }
public function Damage(int amount) { SetHealth(currentHealth - amount); }
public function Heal (int amount) { SetHealth(currentHealth + amount); }
private function SetHealth(int newHealth)
{
currentHealth = newHealth < 0 ? 0 : newHealth > maximumHealth ? maximumHealth : newHealth;
int tempHealth = currentHealth;
for (int i = 0; i < hearts.Length; i++)
hearts[i].SetActive(tempHealth-- >= 0);
if (currentHealth <= 0) GameOver();
}
function GameOver()
{
Debug.Log("collsion detected");
Application.LoadLevel("play 2");
}
function OnTriggerStay2D(autre : Collider2D )
{
Debug.Log("collsion detected");
if (autre.gameObject.tag != "nine") return;
Debug.Log("planet detected");
if (!Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) return;
Damage(1);
}
I'm not all that familiar with JavaScript and this is untested, so it's more like pseudo code, but you get the idea. There were a few big problems with your code. One of them was how you were checking your health, every single frame. You're not the first Unity developer I've seen that thinks you can just through things on the Update function and get away with it. If you keep doing that for every single class, you're performance will start to drop. It's bad practice and organization. You need to use more abstraction in your code.
ins$$anonymous$$d, have a Damage and Heal function. These are simple and public, and both call on the private SetHealth who does the heavy lifting. I used a neccessary ternary operator to clamp the value of currentHealth before we assign it. Ternary operators are awesome and you should look up more about them.
You then had 3 variables for heart. Sure, that may have worked, but what if you wanted to make 5, or 10, or 500 hearts? This new code is much cleaner, uses less lines, and easier to expand upon. I'm using an array to contain the three hearts. I then use the for loop to loop through all of them.
The tempHealth buisness is what I use to deter$$anonymous$$e if we should show the heart or not. If tempHealth starts at 2, the program will check if 2 is greater than zero (which will be true) so the first sprite is visible. It then decreases the 2 to 1 and checks again. Again, the next sprite is visible. It decreases again, and now, tempHealth is 0, so the third heart is invisible. So now, you can easily add 50 hearts if you wanted, without having to change the code.
Also, ins$$anonymous$$d of currentHealth = currentHealth - 1
you can do currenthealth -= 1
or even `currentHealth--;
Answer by m-akrami · Apr 28, 2018 at 02:55 PM
hey , the GetKeyDown method only run once when you press a key down , if you pressing the button before entering the collision this line not going to run :
if (Input.GetKeyDown(KeyCode.Space)) {
currentHealth = currentHealth - 1;
}
you can do this with one bool variable in update function like this :
if (Input.GetKeyDown(KeyCode.Space)) {
spacePressed = true;
}
if (Input.GetKeyUp(KeyCode.Space)) {
spacePressed = false;
}
and then check this bool in your collision function like this:
function OnTriggerStay2D(autre : Collider2D ){
Debug.Log ("collsion detected");
if (autre.gameObject.tag=="nine") {
Debug.Log("planet detected");
if (spacePress) {
currentHealth = currentHealth - 1;
}}}
Your answer
![](https://koobas.hobune.stream/wayback/20220612161651im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
"Keyword already used" error message by voice recognizer script 1 Answer
Trouble setting a parameter with a collision 1 Answer
Keyboard detector 0 Answers
Only destroy one instatnce 1 Answer
Easy sprite finding script 1 Answer