- Home /
 
how to take health/hearth from player when colliding with an object?
I'm working on a 2d sidescrolling shooter, but I can't figure out how to take damage from the player when hitting something (a projectile or an enemy). I have a 4 heart health system and if the player gets hit by something, then 1 heart is gone, if all 4 hearts are gone, the player dies. unfortunately... this doesn't work. Nothing happens to the hearts... Help!
this is the Health Script: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Health : MonoBehaviour { public int PlayerHealth = 4; public int numOfHearts = 4; public int damage = 1; public Image[] hearts; public Sprite fullHeart; public Sprite emptyHeart;
 public GameObject deathEffect;
 public void TakeDamage(int damage)
 {
     PlayerHealth =- damage;
     if (PlayerHealth == 0)
     {
         Die();
     }
     void Die()
     {
         Instantiate(deathEffect, transform.position, Quaternion.identity);
         Destroy(gameObject);
     }
 }
 public void Update()
 {
     if (PlayerHealth > numOfHearts)
     {
         PlayerHealth = numOfHearts;
     }
     for (int i = 0; i < hearts.Length; i++)
     {
         if (i < PlayerHealth)
         {
             hearts[i].sprite = fullHeart;
         }
         else
         {
             hearts[i].sprite = emptyHeart;
         }
         if (i < numOfHearts)
         {
             hearts[i].enabled = true;
         }
         else
         {
             hearts[i].enabled = false;
         }
     }
 }
 public void OnCollisionEnter2D(Collision2D collision)
 {
     if (collision.gameObject.tag == "Projectile")
     {
         PlayerHealth =- damage;
     }
 }
 
               }
I can't figure out what I'm doing wrong... Also here are the scripts of the enemyprojectile and the enemy: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class EnemyProjectile : MonoBehaviour { public float moveSpeed = 7f; public int damage = 100; public GameObject ImpactEffect; Rigidbody2D rb;
 PlayerMovement2 target;
 Vector2 moveDirection;
 void Start()
 {
     rb = GetComponent<Rigidbody2D>();
     target = GameObject.FindObjectOfType<PlayerMovement2>();
     moveDirection = (target.transform.position - transform.position).normalized * moveSpeed;
     rb. velocity = new Vector2 (moveDirection.x, moveDirection.y);
     Destroy(gameObject, 3f);
 }
 void OnTriggerEnter2D (Collider2D col)
 {
     if (col.gameObject.name.Equals("Player"))
     {
         Debug.Log("Hit!");
         Destroy(gameObject);
         Instantiate(ImpactEffect, transform.position, transform.rotation);
     } 
 }
 
               } using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Enemy : MonoBehaviour { public Transform player;
 public int health = 400;
 public GameObject deathEffect;
 public void TakeDamage(int damage)
 {
     health -= damage;
     if (health <= 0)
     {
         Die();
     }
     void Die()
     {
         Instantiate(deathEffect, transform.position, Quaternion.identity);
         Destroy(gameObject);
     }
 }
 
               }
Answer by BastianUrbach · Jul 21, 2020 at 03:57 PM
You got a few =- in there. I'm pretty sure that should be -=. Both are valid C# but they mean different things. A -= B subtracts B from A and assigns the result to A while A =- B assigns -B to A (it should be written as A = -B though to avoid confusion).
Answer by jmailloux11 · Jul 22, 2020 at 12:38 PM
Maybe your other problem is that you have a void named "Die" IN another void.
Try casting it somewhere else.
Hope it was usefull!
Your answer
 
             Follow this Question
Related Questions
Player Health Damage 2 Answers
damage system 0 Answers
Player taking damage on collision. Can't get the script to work!? 1 Answer
Instantiate a projectile on key press "F" key / OnMouseDown! Help!!! 2 Answers
bullet damage problem 1 Answer