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
1
Question by stingman · May 05, 2012 at 07:19 PM · tagactivefindgameobjectswithtaginactiveoncontrollercolliderhit

Help Activating/Deactivating all Game Objects with same tag

The title sums it up. I'm stuck... giving me the error message when colliding with the enemy:

MissingFieldException: Field 'UnityEngine.GameObject[].active' not found.

If I change the code to findwithtag for one enemy then the code works fine. but obviously i have several enemies in my game so I need to figure out how to apply this using an array or something... I know it's probably a simple fix but I'm not seeing it. Any help is appreciated. Here is my code:

 private var playerColor : GameObject;
 var colors = new Color[2];
 var player : GameObject;
 
 function Awake() {
 player = gameObject.FindWithTag("Player");
 player.animation["RedHit"].wrapMode = WrapMode.Once;
 playerColor = gameObject.FindWithTag("ColorFlash");
 }
 
 function OnControllerColliderHit(hit:ControllerColliderHit) {
     
     if (!enabled) 
     return;
     
     var enemyTriggers = GameObject.FindGameObjectsWithTag("EnemyDeathTrigger");
     var controllerScript: RedController = GetComponent(RedController);
         
     if (hit.gameObject.tag == "Enemy") {
     controllerScript.GetHit(); 
     Time.timeScale = 0.00001;
     Physics.IgnoreCollision(gameObject.collider, hit.gameObject.collider); 
     playerColor.renderer.material.color = colors[0];
     player.animation.Stop();
     player.animation.Play("RedHit");
     player.animation["RedHit"].speed = 1.0/Time.timeScale;
     enemyTriggers.active = false;  // THIS IS WHERE MY ERROR COMES FROM
     yield WaitForSeconds(1 * Time.timeScale);
     Time.timeScale = 1.0;
     yield WaitForSeconds(1);
     playerColor.renderer.material.color = colors[1];
     enemyTriggers.active = true;
     Physics.IgnoreCollision(gameObject.collider, hit.gameObject.collider, false);
 }
Comment
Add comment · Show 2
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 Pandiux · May 05, 2012 at 07:22 PM 0
Share

You need to treat the enemyTrigger as an array and loop through it, try using this code

for (var go : GameObject in enemyTriggers){ go.active = false; }

avatar image stingman · May 05, 2012 at 07:29 PM 0
Share

Thank you and Aldo Naletto...works perfectly

1 Reply

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

Answer by aldonaletto · May 05, 2012 at 07:24 PM

enemyTriggers is a GameObject array: you must deactivate its elements one by one in a loop - like this:

   for (var trigger:GameObject in enemyTriggers){
     trigger.active = false;
   }

Do the same to reactivate them:

   for (trigger in enemyTriggers){
     trigger.active = true;
   }

NOTE: Variables declared inside loops or brackets aren't local to them in JS - that's why trigger must not be re-declared in the second for (it's valid anywhere inside the function)

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 stingman · May 05, 2012 at 07:28 PM 0
Share

Thanks, exactly what I needed!

avatar image aldonaletto · May 05, 2012 at 07:32 PM 0
Share

I've made a mistake: the variable trigger must not be redeclared in the second loop (it's valid inside the whole function). I edited my answer to fix this.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Finding all GameObjects with the same tag when some of them are inactive? 4 Answers

Toggling a game object between an active and inactive state 1 Answer

Count >= then object tag becomes active 1 Answer

Destroy gameobject by tag (.js) 3 Answers

How to "touch" a slider via script? 0 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