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 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
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
2
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
Add comment · 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!! $$anonymous$$any 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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How to use if statement properly 1 Answer

Deactivate An Object After Certain Amount of Time 4 Answers

Does inactive Objects eat up Performance? 1 Answer

How do I change a gameobject to be active through a script? 1 Answer

How would I attached a gameobject to a prefab that is spawned at a specific time? 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