'List.Contains() is ALWAYS false

I'm making the game in this tutorial and mostly using their codes. The list.contains() method in unity always returns false for me. I've checked other parts of the code and this seems to be the only problem. I'm making this for my first game jam and I've been stuck on it for a day now, so if you can please help!

    else {
        if (previousSelected == null) { // 3 Is it the first tile selected?
            Debug.Log('6');
            Select();
            
        }
            Debug.Log(GetAllAdjacentTiles().Contains(previousSelected.gameObject));
            
            if (GetAllAdjacentTiles().Contains(previousSelected.gameObject))
            { // 1
                Debug.Log('8');
                SwapSprite(previousSelected.render); // 2
                Debug.Log('3');
                previousSelected.ClearAllMatches();
                previousSelected.Deselect();
                ClearAllMatches();
            } 
            else { // 3
                previousSelected.GetComponent<Tile>().Deselect();
                Select();
            }
        
    }

this is the part where it always returns false:

if (GetAllAdjacentTiles().Contains(previousSelected.gameObject))

also, here's the full code

public class Tile : MonoBehaviour {
private static Color selectedColor = new Color(.5f, .5f, .5f, 1.0f);
private static Tile previousSelected = null;

private SpriteRenderer render;
private bool isSelected = false;

private Vector2[] adjacentDirections = new Vector2[] { Vector2.up, Vector2.down, Vector2.left, Vector2.right };

private bool matchFound = false;



void Awake() {
    render = GetComponent<SpriteRenderer>();
}

private void Select() {
    isSelected = true;
    render.color = selectedColor;
    previousSelected = gameObject.GetComponent<Tile>();
    //SFXManager.instance.PlaySFX(Clip.Select);
}

private void Deselect() {
    isSelected = false;
    render.color = Color.white;
    previousSelected = null;
}
void OnMouseDown() {

    if (render.sprite == null || BoardManager.instance.IsShifting) {
        
        return;
    }

    if (isSelected) { // 2 Is it already selected?
        Debug.Log('5');
        Deselect();
    } 
    else {
        if (previousSelected == null) { // 3 Is it the first tile selected?
            Debug.Log('6');
            Select();
            
        }
            Debug.Log(GetAllAdjacentTiles().Contains(previousSelected.gameObject));
            
            if (GetAllAdjacentTiles().Contains(previousSelected.gameObject)) 
            { // 1
                Debug.Log('8');
                SwapSprite(previousSelected.render); // 2
                Debug.Log('3');
                previousSelected.ClearAllMatches();
                previousSelected.Deselect();
                ClearAllMatches();
            } 
            else { // 3
                previousSelected.GetComponent<Tile>().Deselect();
                Select();
            }
        
    }   

}
public void SwapSprite(SpriteRenderer render2) { // 1
    if (render.sprite == render2.sprite) { // 2
        return;
    }

    Sprite tempSprite = render2.sprite; // 3
    render2.sprite = render.sprite; // 4
    render.sprite = tempSprite; // 5
    //SFXManager.instance.PlaySFX(Clip.Swap); // 6
}
private GameObject GetAdjacent(Vector2 castDir) {
    RaycastHit2D hit = Physics2D.Raycast(transform.position, castDir);
    
    //Debug.Log('1');
    if (hit.collider != null) {
        return hit.collider.gameObject;
        
    }
    return null;
    
}
private List<GameObject> GetAllAdjacentTiles() {
    List<GameObject> adjacentTiles = new List<GameObject>();
    for (int i = 0; i < adjacentDirections.Length; i++) {
        adjacentTiles.Add(GetAdjacent(adjacentDirections[i]));
        //Debug.Log(adjacentDirections.Length);
    }
    Debug.Log(adjacentTiles);
    return adjacentTiles;
    
}
private List<GameObject> FindMatch(Vector2 castDir) { // 1
    List<GameObject> matchingTiles = new List<GameObject>(); // 2
    RaycastHit2D hit = Physics2D.Raycast(transform.position, castDir); // 3
    while (hit.collider != null && hit.collider.GetComponent<SpriteRenderer>().sprite == render.sprite) { // 4
        matchingTiles.Add(hit.collider.gameObject);
        hit = Physics2D.Raycast(hit.collider.transform.position, castDir);
    }
    return matchingTiles; // 5
}
private void ClearMatch(Vector2[] paths) // 1
{
    List<GameObject> matchingTiles = new List<GameObject>(); // 2
    for (int i = 0; i < paths.Length; i++) // 3
    {
        matchingTiles.AddRange(FindMatch(paths[i]));
    }
    if (matchingTiles.Count >= 2) // 4
    {
        for (int i = 0; i < matchingTiles.Count; i++) // 5
        {
            matchingTiles[i].GetComponent<SpriteRenderer>().sprite = null;
        }
        matchFound = true; // 6
    }
}

public void ClearAllMatches() {
    if (render.sprite == null)
        return;

    ClearMatch(new Vector2[2] { Vector2.left, Vector2.right });
    ClearMatch(new Vector2[2] { Vector2.up, Vector2.down });
    if (matchFound) {
        render.sprite = null;
        matchFound = false;
        //SFXManager.instance.PlaySFX(Clip.Clear);
    }
}

}



Solution 1:[1]

The most likely explanation looks to me to be that the raycasts to find adjacent tiles are all hitting the source tile and adding that to the list four times. If so there are three possible solutions:

  1. Go to Edit -> Project Settings -> Physics2d and disable Raycasts Start In Colliders. This will mean raycasts always ignore any collider they begin in, so will cause problems if you need that functionality.
  2. Disable the source object’s collider before doing the raycasts and enable it again afterwards (e.g. by actually disabling the component or moving it to the ignore raycast layer).
  3. Use RaycastAll and iterate through the results ignoring the source object.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sven Viking