Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 mjonasz · Oct 11, 2018 at 05:28 PM · arrayfilter

Filter an array using booleans.

I have a list of checkpoints (tag = checkpoint) of which each has an "ObjectiveControl" script with a bool "playerControl". How do I make a list of all the checkpoints where "playerControl = true"?
I figured I'd start with GameObject[] checkpoint = GameObject.FindGameObjectsWithTag("checkpoint"); for (int i = 1; i < checkpoint.Length; i++) but am not sure how to proceed after that.

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

Answer by SkaredCreations · Oct 11, 2018 at 05:52 PM

Here is:

 List<ObjectiveControl> foundCheckpoints = new List<ObjectiveControl>();
 GameObject[] checkpoint = GameObject.FindGameObjectsWithTag("checkpoint");
 foreach (GameObject obj in checkpoint)
 {
     ObjectiveControl comp = obj.GetComponent<ObjectiveControl>();
     if (comp != null && comp.playerControl)
     {
         foundCheckpoints.Add(comp);
     }
 }
 // Now you can do: foreach (ObjectiveControl comp in foundCheckpoints) { /* do stuff */ }
 

PS: if you use a for() loop instead of foreach() then remember that arrays and lists start with index 0 and not 1:

 for (int i = 0; i < checkpoint.Length; ++i)
 {
    // do stuff...
 }

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 mjonasz · Oct 12, 2018 at 05:14 PM 0
Share

Works perfectly! Big thanks!

avatar image mjonasz mjonasz · Oct 12, 2018 at 06:24 PM 0
Share

What if I want to repopulate the list a couple of times? (The intent is to create a list of checkpoints to which the game object has to to go) I did as you suggested in Start - and it was perfect. But when I put it in update it only worked once. So the game object found the closest checkpoint from the original list and got there. Then it repopulated the list, found the next closest checkpoint and went to it. But after that it did not repopulate the list. Navigation(path[currentCheckpoint].position); if (Vector3.Distance(this.transform.position, path[currentCheckpoint].position) < 1) { closestCheckpoint = path[currentCheckpoint].GetComponent(); if (closestCheckpoint.player2Control)

                     {
                         List<Transform> path = new List<Transform>();
                         GameObject[] checkpoint = GameObject.FindGameObjectsWithTag("checkpoint");
                         foreach (GameObject go in checkpoint)
                         {
                             ObjectiveControl comp = go.GetComponent<ObjectiveControl>();
                             if (comp != null && comp.player1Control)
                             {
                                 path.Add(comp.transform);
                                 Debug.Log(path);
                                 currentCheckpoint = FindClosestCheckpoint();
                             }
                         }
                     }

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

106 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 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 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 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

Add GameObject to an Array. 1 Answer

Why am I getting an error CS0103? 1 Answer

Change an objects Array position dynamiclly in a match 3 game 0 Answers

Arrays don't show up on inspector 1 Answer

How to arrange list 3 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