Generic List 'Contains' always returns false


I am trying out the Contains method for generic Lists, but for some reason it keeps returning false. My goal is to check if a player exists in the Players class.

Here is the code I am testing:

#pragma strict
import System.Collections.Generic;

var playersList = new List.<Players>();

class Players											
	var name : String;											

function Start ()
	//Add Player for testing
	var addPlayer = new Players(); = "Bob";
	//Check if player exists
	var checkPlayerByName = new Players(); = playersList[0].name;
	//Check to see if playersList.Contains(checkPlayerByName) returns true
	print("Player Exists = " + playersList.Contains(checkPlayerByName));
	//Does not return true. Why?

Any ideas?


  1. Please Name your Players class “Player” as it only represents one player, not many “Players”

  2. You ask if a certain instance is in the List and it’s not. You have two instances - addPlayer and checkPlayerByName - and they are different, even if you set their name to be the same.

It’s like if you know two Bobs - they have the same Name, but they are different persons.

If you want something like that, use a generic Dictionary or a Javascript Object associative array to store the players by name.