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 Aidz · Dec 16, 2014 at 04:01 PM · c#gameobjectarraygetcomponentgameobject.find

Find GameObjects with a true boolean and put them in an array?

I'm trying to place a bunch of similar gameobjects into an array that have the same true value as to be 'active'.

Here is my code, which I understand why it is wrong, I'm trying to get a bool and put it into a gameobject array, not going to work, can't figure out how to do this without doing a Gameobject.FindObjects with Tag, and make the other script put the box in the active tag whenever I do something, however that just seems cumbersome, any better ways?

 public GameObject[] activeBoxArray;
 public GameObject[] boxArray; // This is set to 7, and is the amount of boxes I normally have, when I click on them, the boxActive bool is set to true, and then I want to put that in the new array when the game is started by the player.

  if (Input.GetKey(KeyCode.Return))
     {
         for (int i = 0; i < boxArray.Length; i++)
         {
         activeBoxArray[i] = GameObject.Find("Box" + i.ToString()).GetComponent<BoxController>().boxActive == 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
1

Answer by MakinStuffLookGood · Dec 16, 2014 at 04:14 PM

 using System.Linq;
 ..
 ..
 activeBoxArray = FindObjectsOfType<BoxController>().Where(b => b.boxActive).Select(b => b.gameObject).ToArray();

A little bit of Linq and lambdas can do this in one line without any looping, string concatenation, or GetCompontent. This finds all the BoxControllers where boxActive is true, then we select the actual gameObjects rather than the BoxController component, and finally do a ToArray on our resultant set.

If you decide that:

  public GameObject[] activeBoxArray;

could actually just be:

  public BoxController[] activeBoxArray;

then you can remove the select query.

Comment
Add comment · Show 3 · 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 fafase · Dec 16, 2014 at 04:33 PM 1
Share

Despite being right, this answer is not recommended for iOS. This is due to the Linq not being well welcome by Xcode (at least it used to be). So you may have to decompose to something more old school.

 BoxController[] objs = FindObjectsOfType<BoxController>();
 List<GameObject> activeBox = new List<GameObject>();
 foreach(BoxController bc in objs)
 {
     if(bc.boxActive)
     {
         activeBox.Add(bc.gameObject);
     }
 }


avatar image MakinStuffLookGood · Dec 16, 2014 at 04:39 PM 0
Share

Right, I assumed from the use of "Input.Get$$anonymous$$ey($$anonymous$$eyCode.Return)" that this was not being developed for mobiles. In the case of mobiles, you'd really want to avoid the FindObjects and foreach entirely, they can cause GC issues depending on how frequently this thing is being called. And we all know how gross GC lag spikes can be on iOS.

avatar image Aidz · Dec 17, 2014 at 01:57 AM 0
Share

Thanks for the help guys, just wondering is this reusable? The game is blackjack and I want it to be reusable when the game ends, would I just clear the list when the game finishes?

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

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

Related Questions

Using getcomponent with an array 2 Answers

error CS0118: `New_Career.Fame' is a `field' but a `type' was expected 1 Answer

Alter a GameObjects(Clone) array from another script C# 0 Answers

GetComponent with arrays C#? 0 Answers

2D array of GameObjects C# 2 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