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 /
This question was closed Jun 23, 2014 at 12:50 AM by ikelaiah for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by ikelaiah · Apr 07, 2014 at 09:40 AM · listarrayssearch

How can I check values of all array/List quickly

I have a List of gameobjects, each gameobject in the List has an accesible "error" member assigned to it during simulation.

I'd like to trigger a (one) global bool value to true, when the value of "error" in gameobjects in the List is between -0.05 and 0.05.

I'd do this, setup an array of int, matching the number of entries in List, check the entry of List one by one, if condition met, assign 1. If the total of 1s in the int == entry in List than switch a global bool to true.

My approach is rather long and winding. My question is, is there a shorterway?

Comment
Add comment · Show 3
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 Hoeloe · Apr 07, 2014 at 09:48 AM 1
Share

Why not use a list of booleans ins$$anonymous$$d of integers? Or, better still, why not just make a single integer that counts the number of times the condition was met? That way, you don't have to iterate over the list and then the array.

avatar image gjf · Apr 07, 2014 at 09:52 AM 0
Share

without seeing your code, it's not easy to suggest something better (shorter isn't necessarily that). it's unlikely that you'd need an array of int's to store each test condition, but again, without seeing your code, it's not 100% clear what you're trying to achieve.

avatar image ikelaiah · Apr 07, 2014 at 10:00 AM 0
Share

@Hoeloe That's brilliant!!!

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by ArkaneX · Apr 07, 2014 at 10:03 AM

You can use LINQ methods (Any or All), e.g.

 bool allErrorsAreInRange = !gameObjects.Select(x => x.GetComponent<YourComponentWithErrorInfo>().error)
                         .Any(e => e < -0.05f || e > 0.05f);

You need using System.Linq for above code to work.

The drawback of this code is that GetComponent might be called multiple times, up to the number of elements in your list. But in your current code you have the same problem (well - not exactly, since you do your check for each element, while above LINQ code does it until it finds invalid value). If you want to use it often, I would consider creating a list of YourComponentWithErrorInfo instead.

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 ikelaiah · Apr 07, 2014 at 10:22 PM 1
Share

I've accepted your answer based on

"... If you want to use it often, I would consider creating a list of YourComponentWithErrorInfo ins$$anonymous$$d."

Follow this Question

Answers Answers and Comments

23 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

Related Questions

A node in a childnode? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

GetComponent for gameobject list alternative 1 Answer

Adding Item object to Inventory List 0 Answers

Help with C# Arrays / lists / etc - 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