• 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
1
Question by Romano185 · Mar 05, 2012 at 08:49 PM · enemytagattackorderfindwithtag

Attack enemies in order

Hi everybody

I've written some new scripts and they work fine, but there's one problem. Strangly I can only attack the enemy which is first in my Hierarchy panel. After he's gone I can only attack the new first enemy in the Hierarchy panel and so on. These are my scripts, Attackscript is attached to the player and the other 2 to the enemies.

Attackscript:

 var distance = 10;
 var myobject : GameObject;
 var otherobject : GameObject;
 var mytransform : Transform;
 var othertransform : Transform;
 var cooldown : boolean = false;
 var activetexture : GUIStyle;
 var inactivetexture : GUIStyle;
 var usedtexture : GUIStyle;
 var enemyhealth : EnemyHealth;
 static var interval : float;
 
 
 function Start (){
 
 usedtexture = activetexture; 
 interval = 10;
 
 }
 
 function Update (){
 myobject = GameObject.FindWithTag("Player");
 otherobject = GameObject.FindWithTag("Enemy");
 mytransform = myobject.GetComponent(Transform);
 if(otherobject != null){
 othertransform = otherobject.GetComponent(Transform);
 enemyhealth = otherobject.GetComponent(EnemyHealth);
 }
 }
 
 function OnGUI (){
 
 if (GUI.Button (Rect(0,500,100,100),GUIContent("Attack"),usedtexture) && Vector3.Distance(mytransform.position, othertransform.position) < distance && enemyhealth.enemyhealth > 0 && cooldown == false){
 enemyhealth.enemyhealth -= 50;
 cooldown = true;
 Timer();
 }
 }
 
 
 
 function Timer (){
 
 if(cooldown == true){
 usedtexture = inactivetexture;
 yield WaitForSeconds(interval);
 cooldown = false;
 usedtexture = activetexture;
 }
 }

EnemyProximity:

 static var aggroDistance = 10;
 var mytransform : Transform;
 var otherobject : GameObject;
 var othertransform : Transform;
 var glitchCorrector : boolean = true;
 var tickerInterval : boolean = false;
 var tickerRate = 5.0;
 private var nextTick = 0.0;
 static var seppuku : boolean = false;
 var enemyhealth : EnemyHealth;
 var enemyproximityhealth : int;
 
 function Awake (){
 mytransform = gameObject.GetComponent(Transform);
 otherobject = null;
 othertransform = null;
 enemyhealth = null;
 }
 
 function Update () {
 
 if (ClassSelectionScript.classselected == true && otherobject == null && othertransform == null ){
 otherobject = GameObject.FindWithTag("Player");
 othertransform = otherobject.GetComponent(Transform);
 enemyhealth = gameObject.EnemyHealth;
 }
 
 if (othertransform != null && Vector3.Distance(mytransform.position, othertransform.position) < aggroDistance && glitchCorrector == true){
 ArcherHealthScript.curhealth += 10;
 WarriorHealthScript.curhealth += 10;
 MageHealthScript.curhealth += 10;
 glitchCorrector = false;
 }
 
 if (othertransform != null && Vector3.Distance(mytransform.position, othertransform.position) > aggroDistance && glitchCorrector == false){
 glitchCorrector = true;
 }
 
 if(othertransform != null && Vector3.Distance(mytransform.position, othertransform.position) < aggroDistance && tickerInterval == false && HealthScript.curhealth > 0){
 ArcherHealthScript.curhealth -= 10;
 MageHealthScript.curhealth -= 10;
 WarriorHealthScript.curhealth -= 10;
 tickerInterval = true;
 }
 
 if(tickerInterval == true && Time.time > nextTick){
 nextTick = Time.time + tickerRate;
 tickerInterval = false;
 }
 }

EnemyHealth:

 var enemyhealth : int = 100;
 var enemyproximityobject : GameObject;
 var enemyproximity : EnemyProximity;
 var drop : GameObject;
 
 function Start (){
 enemyproximityobject = gameObject;
 enemyproximity = enemyproximityobject.EnemyProximity;
 }
 
 function Update (){
 if(enemyhealth <= 0){
 seppuku = true;
 Destroy(gameObject);
 var instance : GameObject = Instantiate(drop, transform.position+Vector3(0,0.2,0), transform.rotation);
 ExperienceScript.curexp += 10;
 }
 }

I think there's a problem with .FindWithTag. How can I tell Unity that I want ALL the objects with the tag Enemy?

Please help me!

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 Jacek_Wesolowski · Mar 05, 2012 at 09:10 PM

Your assertion is correct. I believe the function you need is FindGameObjectsWithTag().

However, from a code efficiency stanpoint, a better way to achieve similar goal would be to detect the event of an object getting in range and then check if the intruder has the correct tag. You can use Unity's built-in collision detection system to that end. Colliders have a very useful isTrigger flag that helps to use them for simple proximity detection (as opposed to preventing other objects from moving).

Comment
Add comment · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Script only works in the order of the hierarchy 2 Answers

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

Problems with enemy attack script! 0 Answers

Enemy's initial attack doesn't deal damage to player 1 Answer

enemy prefab doesn't follow Player 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