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 post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Alexander_Jensen · Apr 04, 2015 at 08:58 PM · attackduplicateenemieshealth

I can't have more enemies at the same time.

Hey Unity people. when i have one enemy in my scene, both my damage- and healthscript works fine, but when i duplicate my enemy i cant do damage to the original object. Here is my PlayerAttackScript :

 using UnityEngine;
 using System.Collections.Generic;
 using System.Collections;
 
 public class PlayerAttack : MonoBehaviour {
     public int Damage = 10;                    //The Players BaseDamage
     public int MeleeDamage = 10;            //The Players MeleeDamage
     public int RangedDamage = 15;            //The Players RangeDamage
     private float PlayerDistance;            //The Players Distance
     private Transform myTransform;            //The Players transform
     private Transform target;                //The Enemyes transform
     public List<GameObject> EneList;        //The Enemies List
     public GameObject prefab;
     public Transform selectTarget;
     
     void Awake(){
         myTransform = transform;    //Sets MyTransform = Players transform
         PlayerDistance = 2;            //The Distance of the players attack
 
     }
 
     void Start(){
         EneList = new List<GameObject>();
         selectTarget = null;
         AddAllEnemies();
 
     }
     public void AddAllEnemies(){
         GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
 
         foreach(GameObject enemy in go)
             AddTarget(enemy.gameObject);
     }
 
     public void AddTarget(GameObject enemy){
         EneList.Add(enemy);
     }
 
     // Update is called once per frame
     void Update () {
         GameObject ThePlayer = GameObject.FindWithTag("Player");
         PlayerStamina PlayerStam = ThePlayer.GetComponent<PlayerStamina>();
 
         if(Input.GetKeyDown(KeyCode.R)){                            //If R is pressed
             if(PlayerStam.StaminaE == true){                        //If player has enought Stamina
                 if(PlayerDistance <= 2){                            //Checks that the distance is smaller than the MaxDistance
                     gameObject.SendMessage("StaminaHit", 20.0f);
                     GameObject.FindWithTag("Enemy").SendMessage("DamageEnemy", 20.0f);
                 }
             }
         }
         GameObject go = GameObject.FindGameObjectWithTag("Enemy");                        //sets the var target to a transform
         target = go.transform;                                                            //Makes the target = the Enemys transform
 
         PlayerDistance = Vector3.Distance(target.position, myTransform.position);        //Checks the Enemys Distance from Player
     }
 }

And this is my EnemyHealthScript :

 using UnityEngine;
 using System.Collections;
 
 public class EnemyHealth : MonoBehaviour {
     public int EneHealth = 200;
     public int EneCurHealth = 200;
 
     void DamageEnemy (int Damage){
         GameObject TheEnemy = GameObject.FindWithTag("Enemy");
         EnemyAI EnemyAI = TheEnemy.GetComponent<EnemyAI>();
         if(EnemyAI.distance <= 2){
             EneCurHealth -= Damage;
         }
     }
 
 
     // Update is called once per frame
     private void Update () {
 
         if(EneCurHealth <= 0){
             EneCurHealth = 0;
         }
 
         if(EneCurHealth == 0){
         Destroy(gameObject);
         }
     }
 }

Thank you in Advance.

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 DoTA_KAMIKADzE · Apr 05, 2015 at 02:26 PM

That is a bad idea to utilize tags in that manner. Your problem is caused by the fact that you have 2 objects with a Tag "Enemy" or more and you still use find single object with "Enemy" tag and it will just return the first one it finds.

You either need to always reference your EneList or utilize FindGameObject*s*WithTag, though if I were you I'd completely redo this nonsense and start using "Triggers and Colliders" instead.

Furthermore I'd not even use Tags but a custom enum instead, but this one is not critical and more of a personal preference.

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 Alexander_Jensen · Apr 06, 2015 at 05:34 AM 0
Share

I deleted everything and used Triggers ins$$anonymous$$d, everything works fine now. Thank you.

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

Health Bar Script Error 6 Answers

Weapon and enemy health 0 Answers

Show only selected enemy's health bar 1 Answer

I am currently working on a 3-D platformer and am running into an odd problem 0 Answers

Melee Damage script by collision 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