• 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 AllyMonster · Jun 17, 2017 at 12:51 PM · collision2denemydamage

When I kill one enemy, it kills them all.

Hello, I have been researching this problem for a while and I keep coming across people who need to change their variables to non-static. I've tried this and it still isn't working. I am trying to make a 2D game where a set number of enemies come into the room to try and steal the loot from the player, the player can move towards the enemies and click to make them take damage. Everything is working fine except when I kill one enemy all of them die and they die with one hit. I am using c# as I haven't used JavaScript very much. Any help is greatly appreciated. :)

      public class EnemyScript : MonoBehaviour
      {
      private float curHealth = 40f;
      private float maxHealth = 40f;
      private float decHealth = 4f;
     public static int EnemyCount = 0; 
  
  void Start()
      {
  
          EnemyCount++;
          print("Enemy Count = " + EnemyCount);
  }
      
  void Update()
          {        
      if (curHealth <= 0f)
              {
                  destroyobject();
      
              }
      }
      
      void destroyobject()
          {
              Destroy(this.gameObject);
          }
      
      void OnCollisionStay2D(Collision2D collision){
              if (collision.gameObject.tag == "Player" && Input.GetMouseButtonDown(0))
              {
                  curHealth -= decHealth;
                  LevelManager = GameObject.FindObjectOfType<LevelManager>();
                  LevelManager.EnemyDefeated();
      
              }
  }  
 

In the LevelManager script function EnemyDefeated() it keeps track of how many enemy objects are left and when all the enemies are gone, it loads a win screen.

Comment
Add comment
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
1
Best Answer

Answer by DennisWachtendorf · Jun 17, 2017 at 01:25 PM

Are you sure it kills all the players? Or maybe just start to show the winning screen? You call the LevelManager.EnemyDefeated method as soon as you kill one enemy, maybe that's the problem?

Comment
Add comment · Show 2 · 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 AllyMonster · Jun 17, 2017 at 01:38 PM 0
Share

I'm pretty sure it kills all the enemies, the code for the EnemyDefeated function is below.

     public void EnemyDefeated()
     {
         ChestFind.EnemyCount--;
         print("EnemyCount " + ChestFind.EnemyCount);
         if (ChestFind.EnemyCount <= 0)
             LoadLevel("Win Screen");
     }


I also forgot to add a couple of things to the code above. I have added them now. Thanks for the reply :)

avatar image AllyMonster · Jun 18, 2017 at 04:34 AM 0
Share

facepalm

Okay thanks for that. You made me rethink this part of the code and I realised that I had asked it to say that it had defeated an enemy when it had only hurt it. I hadn't picked it up as it was also decreasing the enemycount which was what I was checking. I took the Level$$anonymous$$anager stuff out of here

         if (collision.gameObject.tag == "Player" && Input.Get$$anonymous$$ouseButtonDown(0))
         {
             curHealth -= decHealth;
 
 
         }

and put it here

 if (curHealth <= 0f)
         {
             destroyobject();
             Level$$anonymous$$anager = GameObject.FindObjectOfType<Level$$anonymous$$anager>();
             Level$$anonymous$$anager.EnemyDefeated();
 
         }

Thanks again for the help :D

avatar image
0

Answer by Killerbro389 · Jun 17, 2017 at 04:22 PM

Aren't you missing a curly bracket at the end

Comment
Add comment · Show 2 · 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 NoseKills · Jun 17, 2017 at 04:45 PM 0
Share

Could be but that's probably just a copy-paste mistake. Curlies always come in pairs: if you have an extra one or are missing one, your code won't compile. There's no situation where you could cause a bug by adding a single curly somewhere in the code.

avatar image AllyMonster · Jun 18, 2017 at 04:19 AM 0
Share

Oops, but yea Nose$$anonymous$$ills is right. It was just a copy paste error, my actual code has enough curly brackets.

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

68 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

Related Questions

Different Enemy Lives JavaScript 1 Answer

Collision2D not triggering 1 Answer

How does Collision2D.relativeVelocity Work? 0 Answers

How do I detect which collider is being collided with? 0 Answers

[SOLVED] How to Destroy a rigidbody game object on collision with another gameobject? 4 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