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 /
avatar image
0
Question by Albert-han · Sep 16, 2014 at 09:30 AM · listint

Comparing Ints in Two List

I have 2 list that both contain an int.I want to compare the int in List 1 to the int in List 2.And if they're the same then i want the texture2d in list 2 to be the texture 2d in list 1.Can anyone ple give me an example script of the concept i am looking for.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 fafase · Sep 16, 2014 at 09:40 AM 1
Share

Decomposing your question into distinct line of actions would do half the work:

  • I have 2 list that both contain an int.

  • I want to compare the int in List 1 to the int in List 2.

  • And if they're the same then i want the texture2d in list 2 to be the texture 2d in list 1.

In more technical terms:

 listA as List<Class>
 listB as List<Class>
 
 for(all items in listA){
    if listA[index].int == listB[index].int
       listA[index].texture == listB[index].texture
 }


avatar image Albert-han · Sep 16, 2014 at 09:50 AM 0
Share

Sorry is this javascript,im not experienced in javascript.Thanks alot for answering anyway.

1 Reply

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

Answer by HarshadK · Sep 16, 2014 at 09:40 AM

If you know the value of integer before hand then you can use List.Contains() on both the lists to check if they both contain that integer.

If you want to check if both lists have any common integer then you can do it like:

This code is not tested, but you can use the logic.

 int i=0;
 while(i < List1.Count)
 {
     if(List2.Contains(List1[i]))
     {
         // There is a common item so perform the required action.
     }
     else
     {
         i++;  // We increment the value of i to check for next element from List1.
     }
 }

Comment
Add comment · Show 7 · 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 Albert-han · Sep 16, 2014 at 09:51 AM 0
Share

Im sorry but my int is in the list so i can create more than 1 element with different int.

avatar image HarshadK · Sep 16, 2014 at 09:53 AM 0
Share

Then the provided code is the logic for that. It is the base logic on top of it you can tweak it to create all the effects you want.

avatar image HarshadK · Sep 16, 2014 at 10:07 AM 0
Share

List is just the name of your classes and variables and are not actual collections. You had us think that those are collections of type List.

This can be done as:

 // Instantiating your two classes and create an object for each of them
 List1 list1Object = new List1();
 List2 list2Object = new List2();
 
 // You can compare the variables from those classes using an == operator.
 if(list1Object.list1 == list2Object.list2)
 {
     // Do whatever you want to do
 }
avatar image Albert-han · Sep 16, 2014 at 10:11 AM 0
Share

Sorry this is what i mean.

 [System.Serializable]
 Public class List1
 Public texture2d list1texture;
 Public text list1text;
 Public int list1int;
 }
 
 [System.Serializable]
 Public class List2
 Public texture2d list2texture;
 Public text list2text;
 Public int list2int;
 }

I want it to compare list1int to list2int and if they both match then list2text and list2texture to be list1text and list1texture.

avatar image HarshadK · Sep 16, 2014 at 10:15 AM 0
Share

Logic is still as per comment above.

 // Instantiating your two classes and create an object for each of them
 List1 list1Object = new List1();
 List2 list2Object = new List2();
  
 // You can compare the variables from those classes using an == operator.
 if(list1Object.list1int == list2Object.list2int)
 {
     // Assign the text and texture from List2 to List1.
     list1Object.list1text = list2Object.list2text;
     list1Object.list1texture = list2Object.list2texture;
 }
Show more comments

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

2 People are following this question.

avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

using char to extract integer numbers from a list - [UnityScript] 1 Answer

Can I create a list with an int/float and a string? C# 2 Answers

Convert String to int 1 Answer

Set int to object from 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