Reputation: 99
I made a player with 10 health. each time he gets hit he loses 1 health. The difference can be seen within the health bar. To give the player a chance to survive against waves of enemies I tried to recreate Health pickup.
I made the code so that the player gets healed, so far it triggers when the player walks over the icon, BUT for some reason, the player does NOT get healed?
To make use of all this I have 2 documents of code.
The first one underneath is the code added on the health pickup
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HealthPickup : MonoBehaviour
{
Health health;
public float healthBonus;
void Awake()
{
health = FindObjectOfType<Health>();
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.GetComponent<Player>() == null)
return;
if(health.health < health.maxHealth)
health.health = health.health + (int)healthBonus;
{
Destroy(gameObject);
}
}
}
The following code is the code for the players health.
[Header ("Max/Starting Health")]
public int maxHealth;
[Header ("Current Health")]
public int health;
void Start () {
health = maxHealth;
}
public bool TakeHeal (int amount) {
if (dead || health == maxHealth)
return false;
health = Mathf.Min (maxHealth, health + amount);
if (OnTakeHealEvent != null)
OnTakeHealEvent.Invoke();
return true;
}
As you can see underneath I set the bonus health to 3. Adding 3 health towards the player when he collides with the object.
As you can see the player has a total of 10 health. I try to reduce the health ingame and collide with the object. Yet no health is added towards the user.
As you can see the player has now 7 health (3.5 bars)
When I walk over the healthbar the players health (7) still stays at the same number. Even tho the collider and destroying of the object fully function.
Damage player function
protected virtual void Awake () {
if (Owner == null) {
Owner = gameObject;
}
}
public virtual void OnTriggerEnter2D(Collider2D collider)
{
Colliding (collider);
}
public virtual void OnTriggerStay2D(Collider2D collider)
{
Colliding (collider);
}
protected virtual void Colliding(Collider2D collider)
{
if (!isActiveAndEnabled) {
return;
}
// if what we're colliding with isn't the target tag, we do nothing and exit
if (!collider.gameObject.CompareTag(TargetTag)) {
return;
}
var health = collider.gameObject.GetComponent<Health>();
// If what we're colliding with is damageable / Has health component
if (health != null)
{
if(health.health > 0 && !health.invincible)
{
// Apply the Damage
health.TakeDamage(DamageToCause);
}
Upvotes: 2
Views: 134
Reputation: 20269
You aren't guaranteeing that the Health
instance you are modifying/checking is the same as what is attached to your player. So, when a collision with a Player
occurs, you should get the Health
component from the Player
that you are colliding with.
Also, you already check for overheal in TakeHeal
, so you can just re-use that method:
void OnTriggerEnter2D(Collider2D other)
{
if(other.GetComponent<Player>() == null)
return;
health = other.GetComponent<Health>();
if (health.TakeHeal((int)healthBonus))
{
Destroy(gameObject);
}
}
Upvotes: 4