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 beltmanmarc · Nov 23, 2015 at 08:17 PM · scripting problemprograming

iterate through two lists element by element

I want to iterate through two lists, (listA and listB) element by element. listA element 0, then listB element 0, then ListA element 1, then listB element 1 etc. i tryed to achef this trough a nested for loop (see example). But this code starts with listA element 0, but then all ellements of` listB will pass. What do i have to change to iterate element by element?

  for (int i = 0; i< listA.Count; i++)
         {
             placeHolderPosition = ListA[i];
             Debug.Log("placeholderpositie" + ListA);
 
             for (int t = 0; t<listB.Count; t++)
             {
                 
                 bookPosition = listB[t];
                 Debug.Log("boek" + listB);
             }
            
         }
 
         //float dist = Vector3.Distance(placeHolderPosition, bookPosition);
         //Debug.Log("Distance to other: " + dist);
     }
 
Comment
Add comment · Show 5
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 gjf · Nov 23, 2015 at 03:17 PM 0
Share

first of all, you're iterating through an array which is not the same as a List

second, are both arrays the same size? if so, then you can use the same iterator to check each element ins$$anonymous$$d of nesting the loops.

if they're not, then you'll need to iterate with the larger count and ignore elements in the smaller list that don't exist.

these are technicalities which may be unnecessary depending on what you're actually trying to do... from your questions, that's not clear so the above is the best advice given the lack of information.

avatar image beltmanmarc · Nov 23, 2015 at 04:49 PM 0
Share

thank you for your answer gjf. as you've probably noticed I'm not an experienced programmer. I have declared the lists as follows public List Lista = new List (); in the for loop I use Count ins$$anonymous$$d of Lenght. the lists are the same size. They both contain the positions of game objects. I want to compare them with Vector3.Distance

avatar image gjf beltmanmarc · Nov 23, 2015 at 05:26 PM 0
Share

i should have spotted the listname.Count item which shows that it's actually a List (since you can access them like arrays).

if they're both the same size then all you need to do is something like

 for (int i = 0; i < listSize.Count; i++)
 {
     if (listA[i].Distance == listB[i].Distance)
     {
         // do something
     }
 }

but the distance might not be exact so consider using $$anonymous$$athf.Approximately() for the comparison.

and this (foolishly?) assumes that your List contains the Vector3's ;)

avatar image beltmanmarc gjf · Nov 23, 2015 at 06:28 PM 0
Share

are ListA and ListB brought together in ListSize? Because ListSize does not exist in the current context.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JoshuaMcKenzie · Nov 23, 2015 at 09:10 PM

Just iterate as normal using the greater of the two list counts (if they don't match) and then compare only if the iterator continues to remain less than both list counts, breaking as soon as it fails to save on iterations.

EDIT: you can use Mathf.Min() instead of Max so that you don't need the if check and break

     for(int i = 0; i<Mathf.Min(listA.Count,listB.Count);i++)
     {
             float dist = (listA[i] - listB[i]).magnitude;
 
             //play with the dist here
     }
 
Comment
Add comment · Show 4 · 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 beltmanmarc · Nov 24, 2015 at 12:09 PM 0
Share

Thank you, but It is slightly more complicated. Both lists contain elements of type Vector3 I want to check if these positions correspond. I think the approach you suggest this does not happen The function distance needs a beginning and an end point. The starting point is from ListA, the endpoint from ListB. If the distance between these objects is 0 then a know that there on the right place. Therefore, it is necessary to run through element after element from both lists.

avatar image gjf beltmanmarc · Nov 23, 2015 at 11:07 PM 0
Share

i misunderstood slightly. if your lists contain Vector3's and you want to check the distance between them to be zero or close enough (i.e. they're in the same place), then you might want something like this:

 if ($$anonymous$$athf.Approximately(listA[i], listB[i]))
 {
     // do something
 }

i'm not near a machine to test so ymmv ;)

avatar image Bonfire-Boy gjf · Nov 24, 2015 at 12:17 PM 0
Share

Can't you just do that with the dist variable in your code? if (dist < some_threshold)... and so on. Or am I misunderstanding beltmanmarc's last comment?

Tiny improvement, while I'm here - no need to use $$anonymous$$athf's float functions on ints... $$anonymous$$ath.$$anonymous$$in will do the job.

avatar image beltmanmarc · Nov 24, 2015 at 12:13 PM 0
Share

thank you Joshua$$anonymous$$c$$anonymous$$enzie it works

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity 5.6.1 Pick Up And Read Note Script 3 Answers

Rocket Projectiles in Flight Simulator error 1 Answer

How do I change scenes from my credits to my main menu using a trigger. 1 Answer

How can I make a Dropdown option to be converted on a text to appear somewhere? 0 Answers

Making a circle and input multiple images. 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