• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Simonvriesema · Jul 21, 2020 at 02:48 PM · projectiledamageshooterdiehealth

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 $$anonymous$$tting somet$$anonymous$$ng (a projectile or an enemy). I have a 4 heart health system and if the player gets $$anonymous$$t by somet$$anonymous$$ng, then 1 heart is gone, if all 4 hearts are gone, the player dies. unfortunately... t$$anonymous$$s doesn't work. Not$$anonymous$$ng happens to the hearts... Help!

t$$anonymous$$s 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);
     }
 }

}

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image

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 t$$anonymous$$ngs. A -= B subtracts B from A and assigns the result to A w$$anonymous$$le A =- B assigns -B to A (it should be written as A = -B though to avoid confusion).

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Simonvriesema · Jul 22, 2020 at 12:12 PM 0
Share

I did what you said but nothing changes...

avatar image

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!

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

134 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

I want an enemy to take damage from a projectile (A Certain Layer Named Bullet) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges