• 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 /
  • Help Room /
avatar image
0
Question by kymbers · Jan 11 at 02:14 PM · enemyenemy aienemiesenemy healthenemy damage

How To Destroy/Deactivate one enemy without affecting other enemies of the same type?

Hi, I have a problem, I have a universal enemy script, which I use on all enemies, but as in the script when an enemy dies it is set to deactivate the current script and because of that the first enemy goes perfectly to kill him, but when I arrive at the second I can't attack him because he says the script is disabled and he can't access it, how can I do when I kill an enemy not to affect the main script of all enemies?

  public void TakeDamage(int damage)
     {
         currentHealth -= damage;
         anim.SetTrigger("TakeHit");
 
         if (currentHealth == 80)
         {
             heartRightOne.SetActive(false);
         }
 
         else if (currentHealth == 60)
         {
             heartRightOne.SetActive(false);
             heartRightTwo.SetActive(false);
         }
 
         else if (currentHealth == 40)
         {
             heartRightOne.SetActive(false);
             heartRightTwo.SetActive(false);
             heartMiddle.SetActive(false);
         }
 
         else if (currentHealth == 20)
         {
             heartRightOne.SetActive(false);
             heartRightTwo.SetActive(false);
             heartMiddle.SetActive(false);
             heartLeftTwo.SetActive(false);
         }
 
         else if (currentHealth <= 0)
         {
             Die();
 
             heartRightOne.SetActive(false);
             heartRightTwo.SetActive(false);
             heartMiddle.SetActive(false);
             heartLeftTwo.SetActive(false);
             heartLeftOne.SetActive(false);
         }
 
         //Check if the enemy is facingRight or not to display the damage text popup;
         if (facingRight == true)
         {
             GameObject go = Instantiate(damageText, transform.position, Quaternion.identity);
             go.GetComponent<TextMesh>().text = damage.ToString();
             go.transform.SetParent(gameObject.transform);
             go.transform.localPosition += new Vector3(Random.Range(-textPositionX, textPositionX), Random.Range(+textPositionY, 2), Random.Range(0, 0));
         }
         
         else if (!facingRight)
         {
             GameObject go = Instantiate(damageText, transform.position, Quaternion.identity);
             go.GetComponent<TextMesh>().text = damage.ToString();
             go.transform.SetParent(gameObject.transform);
             go.transform.localPosition += new Vector3(Random.Range(-textPositionX, textPositionX), Random.Range(+textPositionY, 2), Random.Range(0, 0));
         }
     }
 public void Die()
     {
         //Set The Animation For Dead and Stop all other functions / animations;  
         anim.SetBool("Dead", true);
 
         //Instantiate a Gold Prefab;
         GameObject gold = Instantiate(goldPrefab, transform.position, Quaternion.identity);
 
         gold.transform.SetParent(gameObject.transform);
         gold.transform.localPosition = new Vector2(0, goldPositionY);
         gold.transform.localScale = new Vector2(localScaleX, localScaleY);
 
         //Set Player +25 Experience;
         experience.IncraeseExp(25);
 
         //Deactivate The Enemy Collider;
         GetComponent<Collider2D>().enabled = false;
 
         //Destroy The Enemy AI;
         Destroy(hotZone);
         Destroy(triggerArea);
         Destroy(hitBox);
         Destroy(enemyHearts);
 
         //Deactivate The Current Enemy Script;
         this.enabled = false;
     }

Because the script is done this way, when I go to attack the second enemy, I can't kill him anymore If help I can make a video to show the entire script bcs its a big one and to show exactly the problem

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 kymbers · Jan 11 at 04:00 PM 0
Share

I solved the problem, thank you all!

0 Replies

· Add your reply
  • Sort: 

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

161 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 avatar image avatar image avatar image

Related Questions

Enemy dies in editor mode but not in build 2 Answers

How to rotate the enemy in 2D top down 0 Answers

Unity2D Need Help in Multiple Enemies Shooting Script 0 Answers

Sync animation with damage deal 0 Answers

My enemy ai won't stay on the ground help please 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges