Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by dspirelja · May 03, 2018 at 12:25 PM · damagehealth-deduction

Wrong gameObject's health variable decrementing,not the one colliding.

 //  script attached to an empty gameobject
 public void Damaging(int damage)
     {
         enemyHealthScript.currentHealth -= damage;
     }
 // script attached to enemy prefab
 public int maxHealth;
     public int currentHealth;
     private void Start()
     {
         currentHealth = maxHealth;
     }
     private void Update()
     {
         if(currentHealth <= 0)
         {
             Destroy(gameObject);
             return;
         }
     }
 // script attached to the player colliding with the enemy
 private void OnTriggerEnter2D(Collider2D collision)
     {
         speed = 0f;
         InvokeRepeating("DamageWhenColliding", 0f, 1f);
     }
 
     private void DamageWhenColliding()
     {
         damageScript.Damaging(1);
     }
 
     private void OnTriggerExit2D(Collider2D collision)
     {
         target = null;
         speed = 150f;
         CancelInvoke("DamageWhenColliding");
         Debug.Log("coll exit");
 
 

I think i didnt miss anything related to the damage logic.So what happens in my game: Example 1: Only one enemy in scene,everything works fine. Example 2: Two enemies in scene,player collides with the first one in the scene hierarchy "enemy" but he wont lose health,only the second "enemy (1)" loses health which is not even colliding. And if I add a third enemy,only the third one loses health no matter who i collide with,than the other 2 never lose health even if colliding.Sry if its a bad explanation of whats going on.

Comment
Add comment · Show 2
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 dspirelja · May 03, 2018 at 12:26 PM 0
Share

Also one extra question,how could i learn to write better logic for games.how could i practice that in an efficient way?

avatar image Mitihalew · May 05, 2018 at 01:30 AM 0
Share

I am having a very similar issue and haven't been able to find any solid answer. If anyone can help, or even just point us in the right direction, I know I would be very grateful. $$anonymous$$y issue is a little different, as my objects only will explode in the order the are spawned when I add health to the enemy. If the player tries to shoot objects that spawn in second or third, it will blow up the first, second then third first, dropping their health once they get to the first objects position. Not sure if that makes any sense or not.

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.

Follow this Question

Answers Answers and Comments

138 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

Related Questions

Apply Damage Universally 0 Answers

Health bar help 1 Answer

Damage indicator representing incoming damage direction with arrow in UI 2 Answers

Sending Damage and Damage Type Together?? 0 Answers

why wont my player take damage in Unet 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