• 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 IMrYoUSeFI · Jun 21, 2013 at 06:22 PM · gameobject.active

Enable/Disable multiple objects attached to one script

hello guys

I have 4 enemies with the same prefab attached to one script i disabled two of them using the same script by boolean variable that can be controlled from unity interface

using this command

 var enable = true;
 gameObject.active = enable;

now how can i enable one enemy and let the other enemy disable by command from other script

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

3 Replies

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

Answer by IMrYoUSeFI · Jun 21, 2013 at 10:04 PM

I know this method, It didnt work the other script cant find enemy script because the entire objet is disable, I am thinking about disable the enemy script and enable it

And in other case this method didn't help me, because i want to enable a specific object not all of them

i used this code and its work

 var enemyScript = GameObject.FindGameObjectsWithTag("enemy"); 
 enemyScript[3].GetComponent(scriptEnemy).enabled = true;

where the number is the object's order in Hierarchy

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 tolgayahya02 · Sep 12, 2021 at 09:13 PM 0
Share

Thank you so muuchh you are life saver mann!!

avatar image
1

Answer by xKroniK13x · Jun 21, 2013 at 08:17 PM

As Slobdell said, each script will have another instance. This is a way to disable/enable all of the objects...

 function DisableObjects() {
     
     var objectsDisabling : GameObject[];
     objectsDisabling = GameObject.FindGameObjectsWithTag("ChangeStates");  //"ChangeStates" is the TAG in which the game object you are interacting with has... you can change this to whatever you'd like, it's in the Inspector.
   
     for (var obj : GameObject in objectsDisabling)  { 
         obj.active = false;   
     } 
 
 }
 
 function EnableObjects() {
     
     var objectsEnabling : GameObject[];
     objectsEnabling = GameObject.FindGameObjectsWithTag("ChangeStates");  //"ChangeStates" is the TAG in which the game object you are interacting with has... you can change this to whatever you'd like, it's in the Inspector.
   
     for (var obj : GameObject in objectsEnabling)  { 
         obj.active = false;   
     } 
 
 }

As stated in the script, you have to assign the tags correctly on the gameobjects. This is very simple to do, if you don't know how just Google it.

After that, edit this script accordingly (put the right tag in the quotes) and then call on the functions when you want to enable/disable the objects.

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
0

Answer by Slobdell · Jun 21, 2013 at 06:58 PM

Even though it's the same script there is going to be a seperate instance for each gameobject it's attached to. So then you can just get the script from each individual enemy and change them independent of each other. For C#..

 EnemyScript script = enemy.getComponent<EnemyScript>();
 script.enable = true;
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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

How to not stop coroutines when deactivating a GameObject 2 Answers

Activating gameobjects with a collision event is not working. 1 Answer

How do I only show objects near the player 1 Answer

Determining if a gameobject is inactive within prefab in files, NOT in a scene 2 Answers

Unity UI Panels can't use gameobject.activeInHierarchy? 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