Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

find missing items between 2 lists - problem

Discussion in 'Scripting' started by larswik, Jan 27, 2019.

  1. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    So I have 9 GameObjects in a List. I iterate through a Collider[] array that can include up to all of the 9 GameObjects. If any of them are missing when comparing the lists I want to add the missing ones to a new List. I get an error on the last line with the Except().

    I am unsure how to resolve this problem? Here is the code.

    Code (CSharp):
    1. private void FixedUpdate() {
    2.         if(checkCollidersInterval++ % 5 == 0){ // check every 5 frames
    3.             Collider[] hitColliders = Physics.OverlapBox(starCheckerGO.transform.position, starCheckerGO.transform.localScale / 2, Quaternion.identity);
    4.             List<GameObject> starsToLeaveAloneList = new List<GameObject>(); // create a list to hold the unused backgrounds
    5.  
    6.             foreach(Collider c in hitColliders){ // return how many boxs are touching
    7.                 if(c.gameObject.layer == 9){
    8.                     starsToLeaveAloneList.Add(c.gameObject); // add the starBG to a list of found ones. This list should be shorter then the number of starbg
    9.                 }
    10.             }
    11.             if (starsToLeaveAloneList.Count == 9){
    12.                 return; // Nothing should be shiffted so return
    13.             }
    14.             List<GameObject> starsThatCanBeMovedList = new List<GameObject>();
    15.  
    16.             starsThatCanBeMovedList = allTheStars.Except(starsToLeaveAloneList);
    17.         }
    18.     }
    They are both Lists and the error is an IEnumerable vs. List? Any help is appreiciated
     
  2. ->
    Code (CSharp):
    1. starsThatCanBeMovedList = allTheStars.Except(starsToLeaveAloneList).ToList();
    Except returns with an IEnumerable<TSource> which you need to cast to the proper type I think.
     
  3. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    Just tested it and that was it. The cast comes after. I was thinking the fast was something like float x = (float) 5; where you would put the cast just before the assignment. First time comparing lists, thanks for the help!