Hi I’m using a life system that really depends on collisions with the player and the different objects like for example if the player collides with an extra life token he gets one more life and if he gets hit by an enemy or falls down a pit/cliff he loses one life however the system that I have implemented to do so doesn’t work that well especially when I want to reduce lives.
using System.Collections;using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Health : MonoBehaviour
{
[SerializeField] private Transform player;
[SerializeField] private Transform respawnPoint;
public float health = 5f;
public float zeroHearts = 0f;
public Text lifeText
private void Update()
{
lifeText.text = health.ToString();
if (health <= 0)
{
health = zeroHearts;
FindObjectOfType<GameManager>().EndGame();
}
}
private void OnTriggerEnter(Collider col)
{
if (col.transform.tag == "HealthUp")
{
HealthUp(1);
}
if (col.transform.tag == "Enemies")
{
TakeDamage(1);
}
if ((col.transform.tag == "PitDeath"))
{
TakeDamage(1);
}
}
void TakeDamage(int damage)
{
health -= damage;
}
void HealthUp(int heal)
{
health += heal;
}
}
for some reason when the player collides with an enemy, he ends up losing 2 lives instead of 1, and when he falls down a pit/cliff because I made it be a collision box he looses practically all of his lives just by falling instead of losing just 1 again I tried using
however, if a player falls into the pit and then falls again he ends up not losing life and just respawns(which is supposed to happen but he is also supposed to lose a life), I’ve tried to do many things but now I’m just lost at what to do to fix this problem
ps: just in case you want to see the respawn code just in case this might be the problem here it is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Respawn : MonoBehaviour
{
[SerializeField] private Transform player;
[SerializeField] private Transform respawnPoint;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
player.transform.position = respawnPoint.transform.position;
Physics.SyncTransforms();
}
}
}
I managed to fix the pit problem however using the debug.log function I saw that the enemies are somehow being triggered 3 times and now they are removing all 5 lives for some weird reason and now the health system is also having problems in which when you collect the health objects the Debug.Log says that I’ve collected them but the number wont change in the health HUD, however, it changes when we take damage.
using System.Collections;using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
updated Script:
public class Health : MonoBehaviour
{
[SerializeField] private Transform player;
[SerializeField] private Transform respawnPoint;
public float health = 5f;
public float zeroHearts = 0f;
public Text lifeText;
bool inPIt;
bool isGrounded;
//gravity check
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
private void Update()
{
lifeText.text = health.ToString();
if (health <= 0)
{
health = zeroHearts;
FindObjectOfType<GameManager>().EndGame();
}
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded)
{
inPIt = false;
}
}
private void OnTriggerEnter(Collider col)
{
if (col.transform.tag == "HealthUp")
{
HealthUp(1);
Debug.Log("Health increase");
}
if (col.transform.tag == "Enemies")
{
TakeDamage(1);
Debug.Log("Health Decrease");
}
if (inPIt == false)
{
inPIt = true;
TakeDamage(1);
}
}
void TakeDamage(int damage)
{
health -= damage;
}
void HealthUp(int heal)
{
health += heal;
}
}
for the enemies problem - it’s hard to help when you can’t locate the source of cause. It could be many reasons that involves more than the script. e.g. Maybe you have multiple colliders? Maybe you attached two of the same script? Or ran the method somewhere else? If they are triggering 3 times but reduces hp by 5, according to the script attached, it doesn’t seem to be this script’s problem.
As for the healing, add anotherDebug.Log(health);underneath Debug.Log("Health increase"); to see if it’s UI not updating or whether the script is actually calling HealthUp();
yeah even with the debug and using a simple cube with one collider nothing shows what could be the problem the healing shows up countless times in the console but it doesn’t register in the text for some weird reason and some times one pick up registers and the other one doesn’t and the enemies haven’t changed I’m going to have to try and focus on other elements of the game and then come back to this thank you for all your help mikeohc I really appreciate it
Each console entry where that debug shows you can study the lower part of the console window and see the call stack of code leading to that output, and perhaps reason about why you are seeing it more times than you expect.
This probably won’t solve your multiple-hit issue (not sure why it’s happening), but there are a number of changes I’d recommend for clarity:
health is used as an int, so declare it as an int, not a float.
zerohearts seems unnecessary: health value is all that matters to determine alive/dead state, UI representation, etc.
The only thing you should do in Update (out of what you have) is your ground check, because that actually does need to be done every frame. Checking the health value and showing it on the screen should be done in your Trigger function, when they actually change, so they’re only done then, not every frame (this is wasted processing).
When checking multiple tag names, use an if/else if structure, because only one happens at a time. This also saves processing (if even just a little).
I wouldn’t bother with separate health+ and health- functions. Just use one called healthChange and send it 1 or -1. Right after changing it, check for health <= 0. (That said, if you like the clarity of different names of HealthUp and TakeDamage, that’s understandable.)
Also wondering about the if(inPIt == false) line, that doesn’t make sense to me…?
the inPit == false is just to make sure that when you fall off the map that it only registers once and doesn’t keep updating so it only removes 1 life instead of many. ill try to implement your suggestions thank you so much for the advice!