• 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 aosome23 · Jul 18, 2014 at 05:47 AM · foreachsearchlookingfinding

Finding a value in a List

I need to find a value in a List. This is an example code that shows what I would want to do.

 private List<Space> spaces;
 private gridSize = 2.5;
 
 
 public struct Space{
    public Vector3 coor;
    public Vector3 pos;
    public int color;
    public bool isCircle;
    public Space(Vector3 c, Vector3 p, int cr, bool ic){
       coor = c;
       pos = p;
       color = cr;
       isCircle = ic;
    }
 }
 
 private static void CreateList(){
    spaces = new List<Space>();
    for(int x = 0; x < 10; x++){
       for(int x = 0; x < 10; x++){
          for(int x = 0; x < 10; x++){
             coorToPos(new Vector3(x, y, z))
          }
       }
    }
 }
 
 private Vector3 coorToPos(Vector3 cor){
    Vector3 newPos = new Vector3(cor.x * gridSize, cor.y * gridSize, cor.z * gridSize)
    return newPos
 }

So, in detail, what I really want to do is... I want to create a function so if I plug in the coordinate or the position of a point in the list, I would be able to get the color, and isCircle Boolean.

I know a foreach funtion will do this by: private Space FindTheSpace(Vector3 vectorCheck, isPos){ foreach(spacs in Space){ if(!isPos && spcs.coor == vectorCheck) return spcs; else if(isPos && spcs.pos == vectorCheck) return spcs; else return null; } }

However, I don't really want to use an foreach funtion because this is going to be in a foreach funtion. I really don't want it to be a double foreach function because I think it will lag too much....

So anyways, I hope you understood my question. If you did not understand something, please ask me. Thank you for your time and effort to trying to solve this problem I have

Thanks

Comment
Add comment · Show 2
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 zharik86 · Jul 18, 2014 at 06:28 AM 0
Share

Check your function CreateList(). $$anonymous$$aybe, you need write(change your x):

  for(int x = 0; x < 10; x++){
   for(int y = 0; y < 10; y++){ //change your x on y
    for(int z = 0; z < 10; z++){ //change your x on z
     coorToPos(new Vector3(x, y, z))
    }
   }
  }
avatar image aosome23 · Jul 20, 2014 at 07:51 PM 0
Share

I am sorry but I don't quite get what you are trying to say..

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by zharik86 · Jul 18, 2014 at 06:19 AM

If you don't want to use foreach, that, probably, you should use List.Find. It is best of all to look at the link msdn or a resource of a forum of Unity. If I am not mistaken, for search you can write:

  private Vector3 myCoorFind = new Vector3(1, 2, 3);
  ... 
  mySpaces.Find(myItem => myItem.coor == myCoorFind)

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 aosome23 · Jul 18, 2014 at 08:58 PM 0
Share

Thank you!! you are great!

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 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

Related Questions

Debug.Log(files in a directory) problem 0 Answers

Search button filter 1 Answer

Possible to use the Google Custom Search API from within Unity? 1 Answer

How do I cleanly check if there is a specific component in a parent x positions above the current component in the hierarchy? 0 Answers

Can I find all references to a specified game object? 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