Unity how to create an if statement for if the list does nto contain a specific item

At the moment I am doing this

if (!GlobalCellInfo.existingCells.Contains(newCellPosition))

but I am getting problems and I am not sure if it cause by this line or not. I need a good way of checking if teh list contains something and if it does not contain it, I need to execute code, but as far as I know there is no doesNotContain code so I tried this and there were no syntax errors, I need to know if this is correct or not or if there is a better way.

This is correct. Contains will return true or false based on whether the list contains that Item. if it doesn’t contain it will return false, your ! will turn the false into a true and execute whatever is next.

You can easily test this by making a small script like this:

List<int> someList = new List<int>();

if (!someList.Contains(5))
        Debug.Log("Doesn't contain 5");
2 Likes

Okay cool thanks, I must be getting better at this if I can figure that kind of stuff out on my own, I figured that was right but at the same time I am getting weird problems with my script so i was just making sure that was correct. Thanks for the quick reply.