
* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.
* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.
처음으로 카드를 클릭했을 때, 다음 짝이 될 카드를 클릭하지 않았음에도 카드가 다시 뒤집히며
아래의 캡쳐와 같은 오류가 발생합니다. 그 직후부터는 애니메이션은 물론, 짝이 맞는 카드를 제거하거나
짝이 맞지 않은 카드를 다시 뒤집는 등의 기능은 정상적으로 작동하는 상태입니다.
이것은 첨부된 동영상을 확인해보시면 될 겁니다.




작성한 코드 및 에러 메세지
//gameManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;//리스트 섞기
public class gameManager : MonoBehaviour
{
public GameObject card;
public Text timeText;
public GameObject firstCard;
public GameObject secondCard;
float time = 0f;
float blank = 1.4f;
public static gameManager I;
private void Awake()
{
I = this;
}
// Start is called before the first frame update
void Start()
{
int[] rtans = { 0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7};
rtans = rtans.OrderBy(item => Random.Range(-1.0f, 1.0f)).ToArray();//리스트를 무작위로 섞기
string rtanName;
int num = 0;//rtans 리스트의 현재 인덱스를 표시하기 위함
for (int i = 0; i < 4 ;i++ )
{
for (int j = 0; j< 4 ; j++)
{
float x = i * blank -2.1f;
float y = j * blank + -3f;
GameObject newCard = Instantiate(card);//이러면 생성과 동시에 편집하기 편하다.
//생성된 newCard를 cards로 옮긴다.
newCard.transform.parent = GameObject.Find("cards").transform;
newCard.transform.position = new Vector3(x, y, 0f);
rtanName = "rtan" + rtans[num].ToString();//이번 카드의 뒷면에 들어갈 이미지의 이름 생성
//생성한 이름을 토대로, 해당 이미지를 찾아 이번 카드의 뒷면에 집어넣음
newCard.transform.Find("front").GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>(rtanName);
num++;
}
}
}
// Update is called once per frame
void Update()
{
time += Time.deltaTime;
timeText.text = time.ToString("N2");//소수점 2자리까지 스트링 형으로 변환
}
//두 카드가 짝이 맞는지 확인하는 함수. 아니라면 다시 뒤집고, 짝이 맞다면 두 카드를 제거한다
public void isMatched()
{
string firstCardImage = firstCard.transform.Find("front").GetComponent<SpriteRenderer>().sprite.name;
string secondCardImage = secondCard.transform.Find("front").GetComponent<SpriteRenderer>().sprite.name;
if(firstCardImage == secondCardImage)
{
//카드 제거
firstCard.GetComponent<card>().destroyCard();
secondCard.GetComponent<card>().destroyCard();
}
else
{
//다시 뒤집기
firstCard.GetComponent<card>().closeCard();
secondCard.GetComponent<card>().closeCard();
}
//매칭이 끝났으므로 새로운 손패를 받을 수 있게 준비한다
firstCard = null;
secondCard = null;
}
}
//card.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class card : MonoBehaviour
{
public Animator anime;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void openCard()
{
//애니메이터의 변수 isOpen을 true로 변경
anime.SetBool("isOpen",true);
//front 부분을 활성화하고 back 부분을 비활성화
transform.Find("front").gameObject.SetActive(true);
transform.Find("back").gameObject.SetActive(false);
if (gameManager.I.firstCard == null)//지금 연 카드가 짝의 첫번째라면
{
gameManager.I.firstCard = gameObject;//현재 열린 카드를 지금의 카드로 둔다
}
else
{
gameManager.I.secondCard = gameObject;//현재 열린 카드를 지금의 카드로 둔다
gameManager.I.isMatched();//짝이 맞는지 판단하는 함수 호출
}
}
public void destroyCard()
{
Invoke(nameof(destroyCardInvoke), 0.5f);
}
void destroyCardInvoke()
{
Destroy(gameObject);
}
public void closeCard()
{
Invoke(nameof(closeCardInvoke), 0.5f);
}
void closeCardInvoke()
{
anime.SetBool("isOpen",false);//캡쳐 화면에서 언급한 card.cs의 57번째 줄
transform.Find("back").gameObject.SetActive(true);
transform.Find("front").gameObject.SetActive(false);
}
}
Tip 1) </> 아이콘을 눌러 코드박스를 만들어 보세요.
Tip 2) Ctrl+A(맥의 경우 Command+A) 단축키로 코드를 한 번에 선택할 수 있어요!
