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 /
This question was closed Mar 31, 2016 at 05:57 PM by haiderInUnity for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by haiderInUnity · Mar 31, 2016 at 05:10 PM · listoperatorlinqcontains

operator == is not working every time to find if a list contains item in another list

Hi i am trying to see if one list contains any item from another list by this code

 for (int j = 0; j < spawnedCoins.Count; j++) {
                     for (int i = 0; i < connectedTiles.Count; i++) {
                         if (spawnedCoins[j].transform.position.x == connectedTiles[i].x && spawnedCoins[j].transform.position.y == connectedTiles[i].y)
  {    
                             coinsCollected++;
                             Destroy (spawnedCoins [j]);
                             spawnedCoins.RemoveAt (j);
                         }
                     }
                 }
 

However Sometimes it finds a match and destroys the object but then sometimes it does not find a match where it is clearly a match. I have confirmed many times that the x and y values of both elements are the same but still NO match is found and it does not go inside if statement. And thus coinsCollected score is not added and coins are not deleted in game.

I have also used contains like this but with same problems

         for (int j = 0; j < spawnedCoins.Count; j++) {
             if (connectedTiles.Contains (spawnedCoins[j].transform.position)) {
                     coinsCollected++;
                         Destroy (spawnedCoins [j]);
                         spawnedCoins.RemoveAt (j);
             }
         }

Can anyone tell me why this is happening? Thanks

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

  • Sort: 
avatar image
1
Best Answer

Answer by Paul-Jan · Mar 31, 2016 at 05:36 PM

Are x and y floats with fractional values (i.e. non-integers)? In that case, two values that show up as "the same" in the debugger (logs, printf, whatever) might not have, in fact, the exact same underlying value as there is a round off in the number of decimals that is displayed.

Try using Mathf.Approximately to compare the values, or use your own equality definition with an appropriate epsilon.

Or, even beter, directly use Vector3/Vector2 == operator on position. The default Vector2 equality operation in Unity already does a "really close to being equal" check.

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 haiderInUnity · Mar 31, 2016 at 05:57 PM 0
Share

x and y are floats but not their values are always integers. your last advise to use == on vector 2 seems to have worked. I dont know y i just didnt do that in the first place. THanks

Follow this Question

Answers Answers and Comments

40 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

Related Questions

A node in a childnode? 1 Answer

using xml, linq, and lists 0 Answers

Inventory Script. List Contains. I dont know the need bit of code.[UNSOLVED] 2 Answers

How to search a certain variable in a list of a created class (C#) 1 Answer

Create a component list from other list 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