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 Romano185 · Mar 24, 2012 at 08:20 PM · enemytaghierarchyorder

Script only works in the order of the hierarchy

Hi everyone

I've written a script which I use to attack enemies (tagged Enemy). It's attached to a collider with IsTrigger active. It works, but only if you attack the enemies in the exact order in which they're placed in the Hierarchy panel.

 var otherobject : GameObject;
 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 OnTriggerStay(Trigger : Collider){
 if (Trigger.tag == "Enemy" && otherobject == null){
 otherobject = Trigger.gameObject;
 enemyhealth = otherobject.GetComponent(EnemyHealth);
 }
 else {
 otherobject = null;
 enemyhealth = null;
 }
 }
 
 function OnGUI (){
 if (GUI.Button (Rect(0,500,100,100),GUIContent("Attack"),usedtexture) && otherobject != null && cooldown == false){
 enemyhealth.enemyhealth -= 50;
 cooldown = true;
 Timer();
 }
 }
 
 
 
 function Timer (){
 
 if(cooldown == true){
 usedtexture = inactivetexture;
 yield WaitForSeconds(interval);
 cooldown = false;
 usedtexture = activetexture;
 }
 }

How can I make Unity accept all the GameObject which have the tag Enemy?

Comment
Add comment · Show 6
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 cowlinator · Mar 25, 2012 at 01:53 AM 0
Share

I don't know, but why do you require otherobject to be null in OnTriggerStay? This will cause only every other collision to affect the health (no matter how long between collisions).

avatar image Romano185 · Mar 25, 2012 at 10:07 AM 0
Share

I've done that to make sure this script only works on 1 enemy. When a second enemy enters the trigger, the player still attacks the first enemy. But after the first enemy dies, the player can immediately attack the second one. This is why I don't use OnTriggerEnter

avatar image Romano185 · Mar 26, 2012 at 05:23 PM 0
Share

Doesn't this have something to do with the tags? Like that there can just exist one object with a tag?

avatar image Romano185 · Mar 27, 2012 at 05:31 PM 0
Share

If I rename one of the objects he takes object I placed first. I still expect that the tags are the problem.

avatar image Romano185 · Mar 28, 2012 at 05:13 PM 0
Share

So it doesn't have to do something with the name, it has to do something with the order I placed the enemies.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by TheLedworks · Mar 30, 2012 at 02:49 PM

Well, if you are going to be attacking more than one enemy at a time then you will need a reference to more than 1 so use an array to store the enemies that are within range in.

http://unity3d.com/support/documentation/ScriptReference/Array.html

e.g.

var enemies : GameObject[];

function OnTriggerStay(Trigger : Collider) {

 if(trigger.CompareTag("Enemy"))
 {
     add me to array, enemies[];
 }

}

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
avatar image
0

Answer by Romano185 · Mar 30, 2012 at 08:38 PM

It still doesn't work. I rewrote my script like this, using arrays like you said:

 var enemies : GameObject[];
 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 OnTriggerStay(trigger : Collider){
 if (trigger.CompareTag("Enemy")){
 enemies.Push(trigger);
 }
 }
 
 function OnGUI (){
 if (GUI.Button (Rect(0,500,100,100),GUIContent("Attack"),usedtexture) && enemies!=null && cooldown == false){
 enemies.enemyhealth -= 50;
 cooldown = true;
 Timer();
 }
 }
 
 
 
 function Timer (){
 
 if(cooldown == true){
 usedtexture = inactivetexture;
 yield WaitForSeconds(interval);
 cooldown = false;
 usedtexture = activetexture;
 }
 }


But it still just accepts the enemies in a specific order. I think that Unity thinks that there's can only be 1 object with the tag Enemy.

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.

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

Attack enemies in order 1 Answer

Find a child with a name, how to?? 5 Answers

PowerUp mechanics doesn't works 0 Answers

hierachy order is not in any order 2 Answers

How to make an enemy change tags after death. 3 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