Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

List.Contains(null) always returns false in Unity 2019.3

Discussion in 'Scripting' started by LiquidBergmann, Jun 1, 2020.

  1. LiquidBergmann

    LiquidBergmann

    Joined:
    Feb 5, 2019
    Posts:
    6
    As the title says,
    I have a script in which I have a List containing GameObjects. From time to time, I check the list for null values via List.Contains(null) to remove entries of GameObjects that might have been destroyed.

    In Unity 2018.3.22 this method works withoug any problems.
    Code (CSharp):
    1. List<GameObject> holderList = new List<GameObject>();
    2. holderList.Add(gameObject_1);
    3. holderList.Add(gameObject_2);
    4. holderList.Add(null);
    5.  
    6. if(holderList.Contains(null)){
    7.     Debug.Log("Found null values in the holder list");
    8.     holderList.RemoveAll(obj=>obj == null);
    9. }
    Using the same snippet in Unity 2019.3.13 doesn't ever return a true, even if I fill the list with null values.
    Has there been a change in the C# implementation that I'm unaware of or might this be a bug?

    Cheers.
     
    Last edited: Jun 1, 2020
  2. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    Code (CSharp):
    1. if(holderList.Conatins(null))
    You misspelled Contains
    Code (CSharp):
    1. if(holderList.Contains(null))
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Also missing the parens + semicolon from the List constructor invocation.
     
    Yanne065 likes this.
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Anyway, despite the probable transcription errors, the reason I believe your code doesn't work is because List.Contains uses the Equals() method to check equality, but UnityEngine.Object only overrides the '==' operator to detect destroyed objects, not the Equals() method. You'll probably need to write your own for loop that uses '==' to detect destroyed objects, rather than Contains().

    Alternatively, remove the Contains() call entirely and just use the RemoveAll() call that you already have, and check the return value. It will return the number of elements that were removed. So if the return value of RemoveAll is greather than 0, you can print your Debug.Log() statement.
     
  5. LiquidBergmann

    LiquidBergmann

    Joined:
    Feb 5, 2019
    Posts:
    6
    Thanks for your reply and sorry for the typos. This was just to be meant as pseudo-code, didn't read over it to thoroughly.

    Yes, I guessed so for the inner workings, but I find it curious that it changed how it works in the the Unity versions from 2018 to 2019. I solved it with the return value of RemoveAll before I made the post, but thought this might help someone else who might get this problem, since I didn't find any "solutions" for this particular case.
     
    PraetorBlue likes this.