Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Vast342 · Jun 01 at 01:32 AM · enemyattackswordhealth-deductionhealth

Damage not working properly

Currently, I am trying to make a sword attack for a roguelike dungeon crawler I have been working on from a decent while now. I have a sprite for the sword and the animation of it swinging, and the beginnings of a system to make the enemies take damage on attack.

The script works like this:

There is a trigger collider around the player and a script that detects whether an enemy is within the radius using the following few lines of code:

 public static float inRange;
     public static List<GameObject> enemiesInRange = new List<GameObject>();
     void OnTriggerEnter2D(Collider2D other) {
         if(other.gameObject.tag == "Enemy") {
             inRange++;
             enemiesInRange.Add(other.gameObject);
         }
     }
     void OnTriggerExit2D(Collider2D other2) {
         if(other2.gameObject.tag == "Enemy") {
             inRange--;
             enemiesInRange.Remove(other2.gameObject);
         }
     }

There is a function within the enemy's EnemyLogic script that makes it take damage, shown here:

 public float health = 10;
 
 // Some time later:
 
 public void takeDamage (float amount) {
         health = health - amount;
         Debug.Log("TOOK DAMAGE, HEALTH NOW " + health);
     }

Using the list of GameObjects created earlier, I have the following foreach loop to attempt to invoke that method on each enemy, however unsuccessfuly at this time.

 foreach(GameObject enemy in SwordTrigger.enemiesInRange) {
                 EnemyLogic a = enemy.GetComponent<EnemyLogic>();
                 a.takeDamage(attackDamage);
             }

As the title suggests, this is not working at this time, and it seems critical to the entire future of this game's design, I believe this is all the code attributed with the sword attack.

From the game's debug log I can determine that the enemy is registered in the inRange float, and other than that I have no idea.

Please help!

EDIT 1: I have one more insight, the issue is with the takeDamage() method, since it outputs the "TOOK DAMAGE, HEALTH NOW" but the health never goes down. I am now trying to fix that instead.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Vast342 · Jun 01 at 02:01 AM

I have figured out the solution, I forgot to set the attack damage again a while ago after recreating the script after a bug.

Setting the damage back to the intended value as 5 again makes the enemy take damage.

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

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

Why is this script not working? 2 Answers

Enemy dies in editor mode but not in build 2 Answers

OverslapSphere dont detect enemyhealth 0 Answers

My enemy don't take damage. 1 Answer

[Solved] I can't call Method from Referenced Script 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