using System.Collections;
using System.Collections.Generic;
public class MemoryGame : MonoBehaviour
public List<GameObject> cards;
public AudioClip matchSound;
public AudioClip flipSound;
public AudioClip gameOverSound;
private GameObject firstCard, secondCard;
private AudioSource audioSource;
private int matchedPairs = 0;
audioSource = GetComponent<AudioSource>();
for (int i = 0; i < cards.Count; i++)
GameObject temp = cards[i];
int randomIndex = Random.Range(0, cards.Count);
cards[i] = cards[randomIndex];
cards[randomIndex] = temp;
for (int i = 0; i < cards.Count; i++)
cards[i].transform.position = new Vector3(i % 4, i / 4, 0);
public void OnCardClick(GameObject clickedCard)
if (clickedCard.GetComponent<Card>().IsFlipped || clickedCard == firstCard) return;
firstCard.GetComponent<Card>().Flip();
audioSource.PlayOneShot(flipSound);
else if (secondCard == null)
secondCard = clickedCard;
secondCard.GetComponent<Card>().Flip();
audioSource.PlayOneShot(flipSound);
StartCoroutine(CheckMatch());
yield return new WaitForSeconds(1);
if (firstCard.tag == secondCard.tag)
audioSource.PlayOneShot(matchSound);
firstCard.GetComponent<Card>().SetMatched(true);
secondCard.GetComponent<Card>().SetMatched(true);
if (matchedPairs == cards.Count / 2)
audioSource.PlayOneShot(gameOverSound);
Debug.Log("Oyun bitti! Tebrikler!");
firstCard.GetComponent<Card>().FlipBack();
secondCard.GetComponent<Card>().FlipBack();