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
1
Question by Costa_GP · Oct 15, 2015 at 06:36 PM · scripting problemlists

Comparing list values

Hi guys! So I need your help, I have created this list, and I'm trying to avoid inserting duplicated values. I've already tried some options but this isn't going that well. So it would be awesome if you could lend me a hand!

So here it goes the list class

 public class Eating_Progress : IComparable<Eating_Progress>
 {
     public int Fruit_X;
     public int Fruit_Y;
     public int Fruit_Z;
 
     public Eating_Progress(int F_X, int F_Y, int F_Z)
     {
         Fruit_X = F_X;
         Fruit_Y = F_Y;
         Fruit_Z = F_Z;
     }
 
     public int CompareTo(Eating_Progress other)
     {
         if(other == null)
         {
             return 1;
         }
     return Fruit_Z - other.Fruit_Z;
     }
 }



and here's what I'm trying to do

     public List<Eating_Progress> eating_progress;
         
         void Start () {
     
             int log_ = 0;
     
             eating_progress = new List<Eating_Progress>();
     
             eating_progress.Add(new Eating_Progress(1, 1, 1));
             eating_progress.Add(new Eating_Progress(2, 2, 2));
             eating_progress.Add(new Eating_Progress(3, 3, 3));
             eating_progress.Add(new Eating_Progress(4, 4, 4));
             eating_progress.Add(new Eating_Progress(1, 2, 3));
 
 // Checks if there's already that value
             if (!eating_progress.Contains(new Eating_Progress(1,2,3)))
             {
                 eating_progress.Add(new Eating_Progress(1, 2, 3));
             }
     
             foreach (Eating_Progress Fruit_X in eating_progress)
             {
                 log_++;
                 Debug.Log(log_);
             }
         }
 
 

Well for some reason this doesn't really do what I want to do, and just adds that value :/

So thanks for your help! Cheers

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

Answer by landon912 · Oct 15, 2015 at 07:13 PM

@Costa_GP Good question.

You see, in this line:

 if (!eating_progress.Contains (new Eating_Progress (1, 2, 3)))

You're actually passing a (new) reference into the IContainer.Contains() method. In all actuality, it is impossible for a new reference to be equal to an existing reference. Therefore, we must compare the actual contents of the Eating_Progress class to another instead of simply comparing their reference.

How? Well, in checking if the List<> contains an object, the method calls IEquatable.Equals() on both objects. Therefore, we can override that method to compare the important contents of the class instead of their reference.

 public class Eating_Progress : IComparable<Eating_Progress>, IEquatable<Eating_Progress>
 {
     public int Fruit_X;
     public int Fruit_Y;
     public int Fruit_Z;
 
     public Eating_Progress(int F_X, int F_Y, int F_Z)
     {
         Fruit_X = F_X;
         Fruit_Y = F_Y;
         Fruit_Z = F_Z;
     }
 
     public int CompareTo(Eating_Progress other)
     {
         return other == null ? 1 : Fruit_Z - other.Fruit_Z;
     }
 
     public bool Equals (Eating_Progress other)
     {
         return (Fruit_X == other.Fruit_X && Fruit_Y == other.Fruit_Y && Fruit_Z == other.Fruit_Z);
     }
 }

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 Bunny83 · Oct 15, 2015 at 08:30 PM 0
Share

Right, CompareTo is used when sorting the list. CompareTo just returns if one element is higher, lower or equal in regard to the sorting order.

avatar image Costa_GP · Oct 16, 2015 at 08:45 AM 0
Share

ty for your help ;)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

trouble scripting a password list to access scene,Script to check UI Input Field against a list of passwords 1 Answer

Public list are not reflecting in inspector. 1 Answer

My list is overwriting the data it was holding with the newest element added to the list. 2 Answers

Temporary GameObject Movement 0 Answers

Why is my list not filling 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