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 Celsius · Oct 28, 2012 at 05:11 PM · c#arrayenemytagattack

Attacking more than one enemy with same tag, but unity only allows one enemy at a time?

Hi, this problem has been stressing me all night and any help is appreciated. I am using c#. I have two types of enemies, enemy1 and enemy2.

I created an enemy code and an attack code. basically it works fine for one on one situation. But if i say, use ctrl-d and make more of the same enemy, problems occur.

Firstly , i can fight many, but as soon as one dies, I cannot kill the other. ('missing game object' shows up). I target those with enemy1 and enemy2 tag. If either enemy dies, I cannot kill the other.

Secondly if I add many enemies, with same tags. So in this example, i spawn 3 guys, all with enemy1 tag. It seems i cannot kill them in any order I want.

I understand my problem is similar to:

http://answers.unity3d.com/questions/328022/i-need-help-to-attack-various-enemies-with-the-sam.html

but I wasnt unable to convert the javascript to c#.

From my understanding, my code only allows me to select two enemies to fight. If i make them chose from tags, they will randomly chose enemies with my selected tag.(So if i spawn 3 guys with enemy1 tags, unity chooses one of them at random to be my opponent and the other two are untouchable)

I do believe my code would need perhaps some array style fix but I am absolutely lose. Below is the attack code.

 using UnityEngine;
 using System.Collections;
 
 public class atkm1 : MonoBehaviour {
     public GameObject target;
     public GameObject target1;
     public float attackTimer;
     public float coolDown;
 
 
     // Use this for initialization
     void Start () {
         attackTimer = 0;
         coolDown =0.0f;
 
     }
     
     // Update is called once per frame
     
     void Update () {
         
         GameObject go = GameObject.FindGameObjectWithTag("enemy1");
         target = go;
         
         GameObject go1 = GameObject.FindGameObjectWithTag("enemy2");
         target = go1;
         
         
         if(attackTimer > 0)
             attackTimer -= Time.deltaTime;
         if(attackTimer < 0)
             attackTimer = 0;
         if(Input.GetKeyUp(KeyCode.F)) {
         if(attackTimer == 0) {
         Attack();
         attackTimer = coolDown;
             }
         }    
     }
     
     private void Attack() {
 
         float distance = Vector3.Distance(target.transform.position, transform.position);
         float distance1 = Vector3.Distance(target1.transform.position, transform.position);
         
         //calculate damage for enemy1 tag 
         if(distance < 2.5f) {
                 hpP eh = (hpP)target.GetComponent("hpP");
                 eh.AddjustCurrentHealth(-10);
         }
             
         //calculate damage for enemy2 tag            
         if(distance1 < 2.5f) {
                 hpL eh = (hpL)target1.GetComponent("hpL");
                 eh.AddjustCurrentHealth(-10);
         }    
         }
     }

One last thing, I use public so i can 'drag and drop' the enemy objects in for testing purposes. Ofcourse even if i do that, i get the same failed results. Basically, the moment i kill one off, then i get the missing gameobject error and my player is unable to attack. EG. target:enemy1 target1:enemy2

if i kill enemy1,

target:missing object target1:enemy2

any help is muchly appreciated. Basically I have a prefab for an enemy that works and attack scripts that works. I wish to fight more than one enemy with same tag in any order .

Comment
Add comment · Show 1
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 Rati · Oct 28, 2012 at 05:39 PM 0
Share

Hmm, at the begining of your update, you set " target = go;" then "target = go1;" should it not be "target1 = go1;" ?

2 Replies

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

Answer by Jessespike · Oct 28, 2012 at 05:57 PM

Instead of a single GameObject per enemy, I replaced it with a List. You can still drag-and-drop enemies into the component like before for testing. Code not tested, but it shows Lists and arrays a bit.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class atkm1 : MonoBehaviour {
     public List<GameObject> targets;
     public float attackTimer;
     public float coolDown;
 
     // Use this for initialization
     void Start () {
        attackTimer = 0;
        coolDown =0.0f;
         
         GameObject[] enemyTargets = GameObject.FindGameObjectsWithTag("enemy1");
         if (enemyTargets != null)
         {
             foreach(GameObject go in enemyTargets)
             {
                 targets.Add(go);
             }
         }
         
         GameObject[] moreTargets = GameObject.FindGameObjectsWithTag("enemy2");
         if (moreTargets != null)
         {
             foreach(GameObject go in moreTargets)
             {
                 targets.Add(go);
             }
         }    
         
     }
 
     // Update is called once per frame
     void Update () {
                 
            if(attackTimer > 0)
             attackTimer -= Time.deltaTime;
         
            if(attackTimer < 0)
             attackTimer = 0;
         
           if(Input.GetKeyUp(KeyCode.F)) {
                if(attackTimer == 0) {
                    Attack();
                    attackTimer = coolDown;
              }
            }
         
     }
     private void Attack()
     {
         float distance = float.MaxValue;
         if (targets == null) return;
         
         foreach (GameObject target in targets)
         {
             if (target != null)
             {
                 distance = Vector3.Distance(target.transform.position, transform.position);
             
                 if (distance < 2.5f) 
                 {
                     hpP eh = (hpP)target.GetComponent("hpP");
                     if (eh != null)
                     {
                         eh.AddjustCurrentHealth(-10);
                     }
                     
                     hpL eh2 = (hpL)target.GetComponent("hpL");
                     if (eh2 != null)
                     {
                         eh2.AddjustCurrentHealth(-10);
                     }
                 }
             }
         }
     }
     
 }
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 freedom667 · Jan 11, 2017 at 08:27 PM 0
Share

i wrote your some code but. when i kill one enemy, then all enemies dying. what can i do?

avatar image
0

Answer by farooqaaa · Oct 28, 2012 at 05:43 PM

Edit: I forgot that you want to attack multiple targets. For that you will need to make the target variable a generic list (means a dynamic array) and then check every target in the array inside the Attack Function:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class atkm1 : MonoBehaviour {
     public List<GameObject> targets;
     public float attackTimer;
     public float coolDown;
 
 
     // Use this for initialization
     void Start () {
        target = new List<GameObject>();
        attackTimer = 0;
        coolDown =0.0f;
 
     }
 
     // Update is called once per frame
 
     void Update () {
 
        GameObject go = GameObject.FindGameObjectWithTag("enemy1");
        GameObject go1 = GameObject.FindGameObjectWithTag("enemy2");

        if(go != null)
           targets.Add(go);

        if(go1 != null)
           targets.Add(go1);

        if(attackTimer > 0)
            attackTimer -= Time.deltaTime;
        if(attackTimer < 0)
            attackTimer = 0;
        if(Input.GetKeyUp(KeyCode.F)) {
            if(attackTimer == 0) {
                Attack();
                attackTimer = coolDown;
            }
        } 
     }
 
     private void Attack() {

        foreach(GameObject target in targets)
        {
        
           float distance = Vector3.Distance(target.transform.position, transform.position);
           float distance1 = Vector3.Distance(target1.transform.position, transform.position);
 
           //calculate damage for enemy1 tag 
           if(distance < 2.5f) {
              hpP eh = (hpP)target.GetComponent("hpP");
              eh.AddjustCurrentHealth(-10);
           }
 
           //calculate damage for enemy2 tag        
           if(distance1 < 2.5f) {
              hpL eh = (hpL)target1.GetComponent("hpL");
              eh.AddjustCurrentHealth(-10);
           }
 
        }
 
     }
 }



GameObject.FindGameObjectWithTag() method will not always return a GameObject because sometimes there are no objects found with the given tag. You need to make sure it's not null before assigning it.

    GameObject go = GameObject.FindGameObjectWithTag("enemy1");

    // Assign go to target only if it's not null
    if(go != null)
       target = go;
 
    GameObject go1 = GameObject.FindGameObjectWithTag("enemy2");

    // Assign go1 to target only if it's not null
    if(go1 != null)
       target = go1;

Also, in your Attack() function you need to check if target is not null before doing anything:

 private void Attack() {
 
    // If target is null then all the code will not be executed
    if(target == null)
      return;

    float distance = Vector3.Distance(target.transform.position, transform.position);
    float distance1 = Vector3.Distance(target1.transform.position, transform.position);
 
    //calculate damage for enemy1 tag 
    if(distance < 2.5f) {
        hpP eh = (hpP)target.GetComponent("hpP");
        eh.AddjustCurrentHealth(-10);
    }
 
    //calculate damage for enemy2 tag        
    if(distance1 < 2.5f) {
        hpL eh = (hpL)target1.GetComponent("hpL");
        eh.AddjustCurrentHealth(-10);
    } 
 }
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 Celsius · Oct 28, 2012 at 08:29 PM 0
Share

thanks for the fast responses! I have elected to use jessepike's code for now as it has been working well thus far.

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

13 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

Related Questions

how can i make the enemy kill more players one after the other chasing them ??? thank you 0 Answers

C# Enemy list for player to attack 2D 1 Answer

Multiple Cars not working 1 Answer

The name `target' does not exist in the current context 1 Answer

Targetting Script Help 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