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 /
avatar image
0
Question by dogukanerdem000 · Mar 19, 2020 at 12:07 PM · scripting problemloopforeach

Can I call multiple functions from other scripts in a foreach loop?,Can I call multiple functions in a foreach loop?

Hi there,

I am trying to detect and damage enemies in an array of layers (enemyLayers) with a foreach loop by calling "takedamage" functions from other scripts, but so far I've been only able to damage only one of the enemies. So the first line in the foreach loop is working; enemy.GetComponent().TakeDamage(attack1Damage);

but the second one is not working; enemy.GetComponent().TakeDamageBanditBowman(attack1Damage);

I've been trying to solve this problem since yesterday and I did a lot of research on this but couldn't find a solution to my problem, so I am open to all possible solutions. Thanks.

 private IEnumerator attack1DamageEnemies()
     {
         //Detect enemies in range of attack
         Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange,enemyLayers);
 
         yield return new WaitForSeconds(waitToDamageEnemiesA1);
 
         //Damage enemies
         foreach (Collider2D enemy in hitEnemies)
         {
             enemy.GetComponent<BanditMeleeController>().TakeDamage(attack1Damage);
             enemy.GetComponent<BanditBowmanController>().TakeDamageBanditBowman(attack1Damage);
         }
     }


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 rh_galaxy · Mar 19, 2020 at 12:20 PM 1
Share

Do you have Bandit$$anonymous$$eleeController and BanditBowmanController components in Every enemy in the loop? Otherwise there would be exceptions. Do you see any exceptions in the console?

avatar image dogukanerdem000 rh_galaxy · Mar 19, 2020 at 01:24 PM 0
Share

No I don't and that is the main problem, these two are different enemies (Bandit $$anonymous$$elee and Bandit Bowman) so they have different kinds of AI and I didn't want to put all of those scripts to every individual enemy. Also I have some animation going on with them, so I didn't want to create a single script for them to takedamage because I don't know how can I handle the animations that way. Really confused...

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ShadyProductions · Mar 19, 2020 at 01:43 PM

What you need to do is use inheritance and use override methods.

 // This class serves as your base class, and should not be added to any enemy gameobject
 public abstract class Enemy : MonoBehaviour
 {
     public virtual void TakeDamage(int damage)
     {
         // Base damage dealing
     }
 }

 // Put this script on your enemy gameobject
 public class BanditMelee : Enemy
 {
     public override void TakeDamage(int damage)
     {
         // Some other way of taking damage
     }
 }

 // Put this script on your enemy gameobject
 public class BanditBowman : Enemy
 {
     public override void TakeDamage(int damage)
     {
         // Some other way of taking damage
     }
 }

 // The GetComponent<Enemy>() will still return the correct instance.
 // (either BanditMelee or BanditBowman but as it's base class Enemy)
 foreach (Collider2D enemy in hitEnemies)
 {
     enemy.GetComponent<Enemy>().TakeDamage(attack1Damage);
 }

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/inheritance

Comment
Add comment · Show 3 · 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 dogukanerdem000 · Mar 19, 2020 at 02:11 PM 0
Share

One quick question, in my base class should I create all the variables and methods from nothing again? Or should I simply call the functions that I've created to damage enemies? By the way here is one example of take damage function;

 public void TakeDamage(int damage)
     {
         if (invincibleCounter <= 0 && currentHealth > 0)
         {
             currentHealth -= damage;
 
             //Play Hurt Animation
             KnockBack();
 
             if (currentHealth <= 0)
             {
                 Die();
             }
             else
             {
                 invincibleCounter = invincibleLength;
             }
         }
     }
avatar image ShadyProductions dogukanerdem000 · Mar 19, 2020 at 02:15 PM 1
Share

Your base class is the base of all enemies, it will be shared between them. So there you will have all your base functionality like health, shield, damage methods etc.

Then in your subclasses you can choose to override some methods, if you need different logic for that specific subclass.

avatar image dogukanerdem000 ShadyProductions · Mar 19, 2020 at 03:32 PM 0
Share

I can't thank you enough... I've been trying to work this out since yesterday but couldn't managed it and now it all works perfectly and so glad that I've learnt about "inheritance" and "override", thanks again :)

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

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

instantiate in foreach loop only gives last string in list 2 Answers

Simplify this code 3 Answers

How can I do a foreach loop for an array of booleans? 1 Answer

Foreach loop wont run completely 3 Answers

If-else statement executing incorrectly in foreach loop,why? 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