Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by khanzaheen10 · Jun 20, 2021 at 01:48 PM · enemy health

enemy not disappearing even after health is zero please help fast

using UnityEngine;

 public class Health : MonoBehaviour
 {
     [SerializeField] private float startingHealth;
     public float currentHealth { get; private set; }
     private Animator anim;
     private bool dead;
     private enemydestrucatble des;
     private void Awake()
     {
         currentHealth = startingHealth;
         anim = GetComponent<Animator>();
         des = GetComponent<enemydestrucatble>();
     }
     public void TakeDamage(float _damage)
     {
         currentHealth = Mathf.Clamp(currentHealth - _damage, 0, startingHealth);
 
         if (currentHealth > 0)
         {
             anim.SetTrigger("hurt");
             //iframes
         }
         else
         {
             if (!dead)
             {
                 anim.SetTrigger("die");
                 GetComponent<PlayerMovement>().enabled = false;
                 dead = true;
                 if (Collision.gameObject.GetComponent<enemydestrucatble>() != null)
                 {
 
                     Destroy(collision.gameObject);
 
                     Destroy(gameObject);
 
 
 
                 }
             }
         }
     }
     public void AddHealth(float _value)
     {
         currentHealth = Mathf.Clamp(currentHealth + _value, 0, startingHealth);
     }
 }
 
 
 //enemyscript
 
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class enemydestrucatble : MonoBehaviour
 {
     [SerializeField] private float movementDistance;
     [SerializeField] private float speed;
     [SerializeField] private float damage;
     private bool movingLeft;
     private float leftEdge;
     private float rightEdge;
     private Animator ANIM;
 
 
 
     private void Awake()
     {
         leftEdge = transform.position.x - movementDistance;
         rightEdge = transform.position.x + movementDistance;
         ANIM = GetComponent<Animator>();
 
     }
 
     private void Update()
     {
         if (movingLeft)
         {
             if (transform.position.x > leftEdge)
             {
                 transform.position = new Vector3(transform.position.x - speed * Time.deltaTime, transform.position.y, transform.position.z);
             }
             else
                 movingLeft = false;
         }
         else
         {
             if (transform.position.x < rightEdge)
             {
                 transform.position = new Vector3(transform.position.x + speed * Time.deltaTime, transform.position.y, transform.position.z);
             }
             else
                 movingLeft = true;
         }
     }
 
     private void OnTriggerEnter2D(Collider2D collision)
     {
         
         
         if (collision.tag == "Player")
         {
 
             collision.GetComponent<Health>().TakeDamage(damage);
 
 
             if (collision.gameObject.GetComponent<enemydestrucatble>() != null)
             {
 
                 Destroy(collision.gameObject);
 
                 Destroy(gameObject);
 
 
 
             }
 
 
 
 
         }
 
 
 //projectile script
 
 
     }
 }
 
 
 
 using UnityEngine;
 
 public class Projectile : MonoBehaviour
 {
     [SerializeField] private float speed;
     private float direction;
     private bool hit;
     private float lifetime;
     [SerializeField] private float damage;
     private Animator anim;
     private BoxCollider2D boxCollider;
 
     private void Awake()
     {
         anim = GetComponent<Animator>();
         boxCollider = GetComponent<BoxCollider2D>();
     }
     private void Update()
     {
         if (hit) return;
         float movementSpeed = speed * Time.deltaTime * direction;
         transform.Translate(movementSpeed, 0, 0);
 
         lifetime += Time.deltaTime;
         if (lifetime > 5) gameObject.SetActive(false);
     }
     private void OnTriggerEnter2D(Collider2D collision)
     {
         hit = true;
         boxCollider.enabled = false;
         anim.SetTrigger("explode");
 
         if (collision.tag == "Enemy")
         {
             
           if(collision.gameObject.GetComponent<enemydestrucatble>() != null)
             {
 
                 Destroy(collision.gameObject);
 
                 Destroy(gameObject);
 
 
             }
 
            
             
         }
         
 
 
     }
     public void SetDirection(float _direction)
     {
         lifetime = 0;
         direction = _direction;
         gameObject.SetActive(true);
         hit = false;
         boxCollider.enabled = true;
 
         float localScaleX = transform.localScale.x;
         if (Mathf.Sign(localScaleX) != _direction)
             localScaleX = -localScaleX;
 
         transform.localScale = new Vector3(localScaleX, transform.localScale.y, transform.localScale.z);
 
 
     }
 
     private void Deactivate()
     {
         gameObject.SetActive(false);
     }
 
     
 
 
 }

, using UnityEngine;

 public class Health : MonoBehaviour
 {
     [SerializeField] private float startingHealth;
     public float currentHealth { get; private set; }
     private Animator anim;
     private bool dead;
     private enemydestrucatble des;
     private void Awake()
     {
         currentHealth = startingHealth;
         anim = GetComponent<Animator>();
         des = GetComponent<enemydestrucatble>();
     }
     public void TakeDamage(float _damage)
     {
         currentHealth = Mathf.Clamp(currentHealth - _damage, 0, startingHealth);
 
         if (currentHealth > 0)
         {
             anim.SetTrigger("hurt");
             //iframes
         }
         else
         {
             if (!dead)
             {
                 anim.SetTrigger("die");
                 GetComponent<PlayerMovement>().enabled = false;
                 dead = true;
                 if (Collision.gameObject.GetComponent<enemydestrucatble>() != null)
                 {
 
                     Destroy(collision.gameObject);
 
                     Destroy(gameObject);
 
 
 
                 }
             }
         }
     }
     public void AddHealth(float _value)
     {
         currentHealth = Mathf.Clamp(currentHealth + _value, 0, startingHealth);
     }
 }









Comment
Add comment · 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 · Jun 20, 2021 at 01:50 PM 0
Share

try debug logs, check to make sure are destroying the right objects, the are dead, their health below 0, check everything!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Tinglee82 · Jun 20, 2021 at 04:26 PM

Just to confirm what you're expecting out of your code. You're expecting your enemy to disappear ONLY when either one of the 2 situations happen - (1) when player collides with enemy, or (2) when projectile collides into enemy (because I don't see TakeDamage being applied to enemy).

If above if accurate, I'd suggest your try using either the Debugger or Debug log to confirm that the "collider" argument passed in to your OnTriggerEnter2D methods actually belong to the enemy, just to rule out some silly situations.

Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

120 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

Related Questions

Enable script (false) 0 Answers

Why does killing one enemy kill them all? 1 Answer

how to destroy game objects with a raycast and health? 1 Answer

How do i Kill Enemy with Vehicle?? 2 Answers

My wave system isn't working 0 Answers


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