• 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
Question by Chris 31 · Mar 17, 2011 at 03:10 PM · gameobjectbooleanactive

activating game objects with a tag

I have this code

if (turretBaseID==true)
    {
        tb1.active=true;
        tb2.active=true;
        tb3.active=true;
        tb4.active=true;
        tb5.active=true;
        tb6.active=true;
        }

but instead of the the many lines of .active=true, is it possible to have a line writing any gameobject with the tag tb.active=true?

Comment

People who like this

0 Show 0
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
Best Answer

Answer by e-bonneville · Mar 17, 2011 at 03:39 PM

Yes. Use a for loop like this:

In C#:

public GameObject[] tbObjects;

for (int i = 0;i<tbObjects.Length;i++) { tbObjects[i].active = true; }

In JS:

var tbObjects : GameObject[];

for(var i = 0;i<tbObjects.length;i++) { tbObjects[i].active = true; }

This is assuming that your tb objects are references to GameObjects. If they're not, you can replace the array variable type with whatever object you are using. Populate the array with references to your tb objects in the Inspector. If that's not an option, let me know and I'll try to come up with an alternate solution if I have time.

Please note that above script examples are untested and report any errors accordingly.

Comment
networkZombie
Chris 31

People who like this

2 Show 6 · 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 Chris 31 · Mar 17, 2011 at 03:58 PM 0
Share

I don't a lot of experience with arrays so bare with me. In the inspector of the objec the script is attached to it shows Tb Objects with a drop down showing size = 0. how do I reference this to the tb objects?

avatar image e-bonneville · Mar 17, 2011 at 04:14 PM 0
Share

You need to set the size to the amount of objects you have (in your case, set 0 to 6), then drag each object into a slot that matches its number-1. Because arrays are zero-based (start counting on 0), you'll want to match up tb1 with position 0, tb2 with position 1, tb3 with position 2, etc.

avatar image Chris 31 · Mar 17, 2011 at 04:42 PM 0
Share

Thanks that has got it working and showing it as active, but when I try to get in as active = false it keeps showing as active.

avatar image e-bonneville · Mar 17, 2011 at 04:46 PM 0
Share

Replace the tbObjects[i].active = true; line with tbObjects[i].active = false; and it should work. :)

avatar image Chris 31 · Mar 17, 2011 at 05:11 PM 0
Share

good work my man, that's been causing me problems for weeks!! Many thanks :D

Show more comments

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

No one has followed this question yet.

Related Questions

If you disable a gameobject, does an InvokeRepeating loop end or pause? 3 Answers

gameObject.active still draws object 3 Answers

Disable / enable script 2 Answers

how can i get a pause screen to work 0 Answers

Turning a menu on and off onclick in hololens 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