Memory game logic

Hi All!
I’m making a 2d memory game with unity.
I have an array with 6 + 6 sprites that I randomize and Instantiate in a wall of 4 * 3 sprites.
Every sprite has a C# script attached to:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Card : MonoBehaviour
{
    public Sprite card;
    public Sprite filledCard;

    void Awake()
    {
       this.GetComponent<SpriteRenderer>().sprite = card;
    }

    public void ShowCard()
    {
        this.GetComponent<SpriteRenderer>().sprite = filledCard;
    }

    public void HideCard()
    {
        this.GetComponent<SpriteRenderer>().sprite = card;
    }
}

And I have another C# script ah controls the logic of the game:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Text.RegularExpressions;

public class MemoryGame : MonoBehaviour
{
    public GameObject container;
    public GameObject cards;

    public GameObject[] filledCards = new GameObject[12];

    public float timer;
    public int count;
    public string cardName = null;  

    void Start()
    {
        //InstantiateCards();
        InstantiateFilledCards();

        timer = 0.0f;
        count = 0;
    }

    void Update()
    {
        RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);

        if (Input.GetMouseButtonDown(0) && hit.collider != null)
        {
            string card = hit.collider.name;    
            GameObject.Find(hit.collider.name).GetComponent<Card>().ShowCard();

            if (cardName != null)
            {
                string name1 = Regex.Replace(card, @"[\d-]", string.Empty);
                string name2 = Regex.Replace(cardName, @"[\d-]", string.Empty);

                if (name1 == name2)
                {
                    Destroy(GameObject.Find(card));
                    Destroy(GameObject.Find(cardName));
                }
                cardName = null;
            }
            else
            {
                cardName = card;
            }
        }
    }

    //void InstantiateCards()
    //{
    //    for (int y = 0; y < 3; y++)
    //    {
    //        for (int x = 0; x < 4; x++)
    //        {
    //            GameObject card = Instantiate(cards, new Vector3(x * 3.5f - 5.0f, y * 3.5f - 3.5f, 0.0f), Quaternion.identity) as GameObject;
    //            card.transform.parent = container.transform;
    //        }
    //    }
    //}

    void InstantiateFilledCards()
    {
        ShuffleArray(filledCards);

        int i = -1;

        for (int y = 0; y < 3; y++)
        {
            for (int x = 0; x < 4; x++)
            {
                i++;
                GameObject filledCard = Instantiate(filledCards_, new Vector3(x * 3.5f - 5.0f, y * 3.5f - 3.5f, 0.0f), Quaternion.identity) as GameObject;_

filledCard.transform.parent = container.transform;
filledCard.name = filledCard.GetComponent().sprite.name + i.ToString();
}
}
}

public static void ShuffleArray(T[] arr)
{
for (int i = arr.Length - 1; i > 0; i–)
{
int r = Random.Range(0, i + 1);
T tmp = arr*;*
arr = arr[r];
arr[r] = tmp;
}
}
}
Now I have a problem with the update:
void Update()
{
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);

if (Input.GetMouseButtonDown(0) && hit.collider != null)
{
string card = hit.collider.name;
GameObject.Find(hit.collider.name).GetComponent().ShowCard();

if (cardName != null)
{
string name1 = Regex.Replace(card, @“[\d-]”, string.Empty);
string name2 = Regex.Replace(cardName, @“[\d-]”, string.Empty);

if (name1 == name2)
{
Destroy(GameObject.Find(card));
Destroy(GameObject.Find(cardName));
}
cardName = null;
}
else
{
cardName = card;
}
}
}

If I click over a sprite, it changes from card to filledCard sprite and compares the names without the number, but the cardName in inspector don’t change at first click but it changes at second click, so the sprites are not destroyed until a third click…
Any idea?

Don’t compare object names. Create a field in your Card class for the name. Better yet use something else for comparing cards, strings are 100% unnecessary here. Integer would be better, depending on what you want you can also use enum.