Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Jan 06, 2015 at 08:56 AM by NickvanDokkum for the following reason:

fixed it myself

avatar image
1
Question by NickvanDokkum · Dec 17, 2014 at 11:18 AM · c#collisionlistforeach

foreach loop in OnCollisionStay

greetings i'm trying to make hp for a wall, but i want to have each enemy hitting the wall doing damage separately (and having a separate attackcooldown timer for each one), does anyone know how i can make one?

i have this:

     private int HP;
     private List<Transform> enemies = new List<Transform>();
 
     void OnCollisionEnter(Collision hit){
         enemies.Add (hit.gameObject);
     }
 
     void OnCollisionStay(Collision collide){
         if (collide.gameObject.tag == "Enemy2") {
             HP -= 10;
         } else if (collide.gameObject.tag == "Enemy1") {
             HP -= 5;
         }
         if (HP <= 0) {
             Destroy(this.gameObject);
         }
     }
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

  • Sort: 
avatar image
0

Answer by EvilTak · Dec 17, 2014 at 12:36 PM

I didn't get what you meant by having a separate timer, but what you can probably do is add a script to your enemies (if you don't already have one). Then add a public int wallDamage to it and subtract it from your wall's HP on collision!

 void OnCollisionStay(Collision collide){
   Enemy enemy = collide.gameObject.GetComponent<Enemy>(); //Enemy is the script in which you added the wallDamage int
   if(enemy != null)
     HP -= enemy.wallDamage;
   if(HP <= 0)
     Destroy(this.gameObject);
 }

That way you don't need those tedious tags!

Comment
Add comment · Show 1 · 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 NickvanDokkum · Dec 17, 2014 at 12:44 PM 0
Share

what i ment with seperate timers is that i don't want them to attack every frame. my code isn't the problem, i just don't know how i can create a timer for every enemy, and then do damage once after the timer hit's 0, then it resets to (let's say) 2 seconds.

Follow this Question

Answers Answers and Comments

28 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

Related Questions

C# Trouble testing whether a list of game objects contains a specific game object 1 Answer

Is there a way to organize tracking of collisions? 0 Answers

C# Problem Creating List of Texture2Ds 0 Answers

null reference exception when I already checked if this was null 1 Answer

foreach a array inside a generic list 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