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 /
avatar image
0
Question by LiOn0X0HeaRt · Jul 14, 2014 at 06:27 AM · playereventbroadcast

One script 2+ Enemies simple one

Hi i wanted to make a script that works on all enemies when the player hit one of them it was a lot harder than i though i want to reach at least the currentHealth on each enemy without making tons of unwanted lines i ended up in a simple dummy script but i tried

 using UnityEngine;
 using System.Collections;
 
 public class PlayerAttack : MonoBehaviour {
     public bool animation1Allowed;
     public p2Animation Player2;
     public int number;
     public GameObject enemy1;
     public GameObject enemy2;
 
 
 void start()
     {
 
         
 
 
     }
     public void OnTriggerEnter(Collider collisionInfo) {
     
         if (collisionInfo.gameObject.CompareTag ("Enemy")) 
         {
                         if (Player2.animationAttackFrame == true) {
                                 animation1Allowed = false;
                             Player2.animationAttackFrame = false;
                 number = 1;
                            damage(5);      
                         }
 
                 }
 
         if (collisionInfo.gameObject.CompareTag ("Enemy1")) 
         {
             if (Player2.animationAttackFrame == true) {
                 animation1Allowed = false;
                 Player2.animationAttackFrame = false;
                 number = 1;
                 damage(5);      
             }
             
         }
  }
     public void damage(int DOP)
     {
         enemy1.GetComponent<CEnemy1Ai> ().currentHealth -= DOP;
 
 }
     public void damage1(int DOP)
     {
     
         enemy2.GetComponent<CEnemy2Ai> ().currentHealth -= DOP;
     }
 }
 
 
 

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
Best Answer

Answer by Andres-Fernandez · Jul 14, 2014 at 06:38 AM

If you are looking for a way to make all the enemies to substract some health points when something occurs (the player collides with one of them) you have a couple options.

The first one is to have a reference to all enemy objects inside an array and use that array to call your damage function. Something like:

 public GameObject enemies[];
 
  public void OnTriggerEnter(Collider collisionInfo) {
  
         if (collisionInfo.gameObject.CompareTag ("Enemy")) 
         {
              for (int i = 0; i < enemies.Length; i++) {
                 damage(enemies[i], DOP);
              }
         }
 }

And then your damage function looks like this:

 public void damage(GameObject obj, int DOP)
 {
    obj.GetComponent<CEnemy1Ai> ().currentHealth -= DOP;
  
 }

The downside of this is that you need to register all enemies whenever you create or destroy an enemy.

There is another way a bit more elegant and actually better, as it doesn't require you to manage an enemies array, and is using events. You can have the player send an event whenever it collides with an enemy and all enemies handle that event within their own code (and substract the DOP). I recommend you to check the Event system tutorials.

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 LiOn0X0HeaRt · Jul 14, 2014 at 02:25 PM 0
Share

I still don't understand how could
if (collisionInfo.gameObject.CompareTag ("Enemy")) work for 2 enemies i tried to understand but it still will work whenever i hit the tagged enemy enemy and he is only 1 all i want is 1 script attached to 2 enemies and they work completely separate when i hit one of them by playerAttack script

avatar image Andres-Fernandez · Jul 15, 2014 at 06:45 AM 0
Share

Wait, I got the question completely wrong. What you meant is that you want to use just one function to make any enemy substract some health points? I thought you wanted it to make all the enemies to substract some hp.

If that's the case, then my answer is wrong, as it calls damage function for every enemy. What makes my answer wrong is the for loop that calls damage function for all enemies! (the CompareTag("Enemy") detects just one enemy, but the for loop triggers all the enemies).

To call damage function only in the enemy you collided with, I would place the damage function inside the enemy script (the one attached to the enemy) and inside your triggerEnter function call Send$$anonymous$$essage. It would look like this:

 public void OnTriggerEnter(Collider collisionInfo) {
  
         if (collisionInfo.gameObject.layer == Layer$$anonymous$$ask.NameToLayer ("Enemy")) 
         {               collisionInfo.gameObject.Send$$anonymous$$essage("damage", DOP, Send$$anonymous$$essageOptions.DontRequireReceiver);
         }
 }

You just check if you collided with an object in the "Enemy" layer (which is the one your enemies will be in) and tell it to call function damage with DOP. Then, add the damage function in your CEnemyAI script:

 public void damage(int DOP) {
    currentHealth -= DOP;
    // rest of stuff
 }

This way, your PlayerAttack doesn't need a function for each enemy. It just tell whatever it collides with to call its own function damage.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Text displays during animation 1 Answer

Cleanest way to decouple Manager class with events 1 Answer

button to select what player shoots 0 Answers

switching scripts on and off 1 Answer

Event.current.Use makes GUI disapear ONLY in standalone 2 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