• 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 HugoOnCaffeine · 3 days ago · damagehealth

why does the enemy instakill

i am currently in early stages of a survival game, but i have a problem that the enemy instakills me and i instakill the ememy. Everything should work but it just doesn't, does someone know what is wrong?

 using UnityEngine.UI;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class PlayerHealthScript : MonoBehaviour
 {
     public int CurrentHP = 100;
     public int MaxHP = 100;
     public Text ValueText;
     
     void Start()
     {
         CurrentHP = MaxHP;
     }
 
     void Update()
     {
         if(CurrentHP < 0)
         {
             Die();
         }
             ValueText.text = CurrentHP.ToString();
     }
     
     public void Die()
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
     }
     
     public void DealDamage(int EnemyDamage)
     {
         CurrentHP =- EnemyDamage;
     }
 
 }
 
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CollisionDetection : MonoBehaviour
 {
     public int EnemyHealth = 50;
     public GameObject HitParticle;
     public Transform parPos;
     public int EnemyDamage = 1;
 
     void Update()
     {
         if(EnemyHealth < 0)
         {
             EnemyDie();
         }
     }
     
     public void TakeDamage(int WeaponDamage)
     {
         EnemyHealth =- WeaponDamage;
         Instantiate(HitParticle, parPos.position, parPos.rotation);
     }
 
     void EnemyDie()
     {
         Destroy(gameObject);
     }
 
     void OnTriggerEnter(Collider PlayerDam)
     {
         if(PlayerDam.gameObject.tag == "Player")
         {
             PlayerDam.GetComponent<PlayerHealthScript>(). DealDamage(EnemyDamage);
         }
     }
     
 }
 
Comment

People who like this

0 Show 1
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 logicandchaos · 3 days ago 0
Share

CurrentHP =- EnemyDamage;

CurrentHP =- EnemyDamage;

These two lines are why, you are not subtracting here, you are setting it to a negative value, which is lower than 0 resulting in instant death.

CurrentHP equals negative EnemyDamage is what you have, what you want is:

CurrentHP -= EnemyDamage;

CurrentHP -= EnemyDamage;

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by kygen007 · 3 days ago

@HugoOnCaffeine Here is the corrected code for the PlayerHealthScript and CollisionDetection classes:

 using UnityEngine.UI;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class PlayerHealthScript : MonoBehaviour
 {
     public int CurrentHP = 100;
     public int MaxHP = 100;
     public Text ValueText;
 
     void Start()
     {
         CurrentHP = MaxHP;
         UpdateHealthText();
     }
 
     void Update()
     {
         if (CurrentHP <= 0)
         {
             Die();
         }
     }
 
     public void Die()
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
     }
 
     public void DealDamage(int EnemyDamage)
     {
         CurrentHP -= EnemyDamage;
         UpdateHealthText();
     }
 
     private void UpdateHealthText()
     {
         ValueText.text = CurrentHP.ToString();
     }
 }
 
 
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CollisionDetection : MonoBehaviour
 {
     public int EnemyHealth = 50;
     public GameObject HitParticle;
     public Transform parPos;
     public int EnemyDamage = 1;
 
     void Update()
     {
         if (EnemyHealth <= 0)
         {
             EnemyDie();
         }
     }
 
     public void TakeDamage(int WeaponDamage)
     {
         EnemyHealth -= WeaponDamage;
         Instantiate(HitParticle, parPos.position, parPos.rotation);
     }
 
     void EnemyDie()
     {
         Destroy(gameObject);
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.tag == "Player")
         {
             other.GetComponent<PlayerHealthScript>().DealDamage(EnemyDamage);
         }
     }
 }
 


**The changes made in PlayerHealthScript include:

Adding a call to UpdateHealthText() in the Start() method to initialize the health UI text. Changing the check in the Update() method to if (CurrentHP <= 0) to handle cases where the player's health drops exactly to 0. Updating the DealDamage() method to subtract the enemy's damage from the player's health and then update the health UI text. The changes made in CollisionDetection include:

Changing the check in the Update() method to if (EnemyHealth <= 0) to handle cases where the enemy's health drops exactly to 0. Updating the TakeDamage() method to subtract the weapon's damage from the enemy's health and then instantiate the hit particle effect. Updating the OnTriggerEnter() method to use the other parameter instead of PlayerDam and to check for the "Player" tag on the other object.**

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 kygen007 · 3 days ago 0
Share

The problem with the code seems to be in the DealDamage() method of the PlayerHealthScript class.

The operator used for decreasing the player's health is -= instead of -, causing the damage dealt by the enemy to add up instead of subtracting from the player's health.

To fix this issue, replace the line CurrentHP =- EnemyDamage; with CurrentHP -= EnemyDamage; in the DealDamage() method.

Similarly, in the TakeDamage() method of the CollisionDetection class, the operator used for decreasing the enemy's health is -= instead of -, which leads to the enemy being destroyed instantly. To fix this, replace the line EnemyHealth =- WeaponDamage; with EnemyHealth -= WeaponDamage;.

With these changes, the player and the enemy should no longer be able to instakill each other.

avatar image

Answer by HugoOnCaffeine · 3 days ago

thanks, sometimes im pretty dumb

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

158 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 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

Photon Combat script does not work 0 Answers

How does the player take damage if collided with enemy? 1 Answer

Health below zero but shows negative numbers 1 Answer

Object Not Recieving Damage 3 Answers

Expecting ) found = 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