- Home /
Find the differences between two lists
Browsing around google it seems that you can use this:
List<string> result = list1.Except(list2).ToList();
to compare the differences between two lists. So I added my variables in and gave it a try and came up with this:
void OnEnable() {
List<Inventory> result = playerCurrentInventoryList.Except(allInventoryList).ToList();
CompareDifferences();
}
void CompareDifferences() {
foreach (Inventory _item in result)
Debug.Log ("result differences iventory" + _item.Name);
}
No debug.logs show up in the console. The playerCurrentInventoyList has a few items in it while the allInventoryList is empty. So my expected differences would be all the items in playerCurrentInventoryList.
My efforts in comparing the two lists based on the example from the google search does not work. Is anyone willing to offer some suggestions of what I am doing wrong?
thanks.
Answer by Bunny83 · Nov 19, 2013 at 12:56 AM
Uhm, you declare a local variable result
in OnEnable, so what ever result you use in CompareDifferences is not the one in OnEnable.
You probably just want to remove the local variable declaration
Your answer
![](https://koobas.hobune.stream/wayback/20220613122157im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
List has strange values at run time 1 Answer
Find specific prefab in a list 2 Answers