
* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.
* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.
if (Game_Manager.Instance.first_card == null) 구문을 사용해서
카드를 선택했을때 First_card에 데이터가 비어있다면 그 데이터를 First_card에 넣고 다음 고른 카드를 Second_card 에 넣으려고 했는데
캡처사진 처럼 오류가 나오면서 데이터가 들어가질 않습니다,,

작성한 코드 및 에러 메세지
// 카드 스크립트
using System.Globalization;
using UnityEngine;
public class Card : MonoBehaviour
{
public int idx = 0;
public SpriteRenderer front_img;
public GameObject front;
public GameObject back;
public Animator anim;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void setting(int number)
{
idx = number;
front_img.sprite = Resources.Load<Sprite>($"rtan{idx}");
}
public void open()
{
anim.SetBool("isOpen", true);
front.SetActive(true);
back.SetActive(false);
if (Game_Manager.Instance.first_card == null)
{
Game_Manager.Instance.first_card = this;
}
else
{
Game_Manager.Instance.second_card = this;
Game_Manager.Instance.matched();
}
}
public void destroy_cards()
{
Invoke("destroy_cards", 1f);
}
void destroy_card_delay()
{
destroy_cards();
}
public void close_cards()
{
Invoke("close_cards_delay",1f);
}
void close_cards_delay()
{
anim.SetBool("isOpen", false);
front.SetActive(false);
back.SetActive(true);
}
}
// 게임 매니저 스크립트
using UnityEngine;
using UnityEngine.UI;
public class Game_Manager : MonoBehaviour
{
public static Game_Manager Instance;
float time = 0;
public Card first_card;
public Card second_card;
public Text time_txt;
// Start is called once before the first execution of Update after the MonoBehaviour is created
private void Awake()
{
if(Instance = null)
{
Instance = this;
}
}
void Start()
{
}
// Update is called once per frame
void Update()
{
time += Time.deltaTime;
time_txt.text = time.ToString("N2");
}
public void matched()
{
if (first_card.idx == second_card.idx)
{
first_card.destroy_cards();
second_card.destroy_cards();
}
else
{
first_card.close_cards();
second_card.close_cards();
}
first_card = null;
second_card = null;
}
}
// board 스크립트
using System.Linq;
using Unity.Burst.Intrinsics;
using UnityEngine;
public class Board : MonoBehaviour
{
public GameObject card;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
int[] arr = { 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7 };
arr = arr.OrderBy(x => Random.Range(0f, 7f)).ToArray();
for(int i=0; i< 16;i++)
{
GameObject GO = Instantiate(card,this.transform);
float x = i % 4 * 1.4f - 2.1f;
float y = i / 4 * 1.4f - 3f;
GO.transform.position = new Vector2 (x,y);
GO.GetComponent<Card>().setting(arr[i]);
}
}
// Update is called once per frame
void Update()
{
}
}
Tip 1) </> 아이콘을 눌러 코드박스를 만들어 보세요.
Tip 2) Ctrl+A(맥의 경우 Command+A) 단축키로 코드를 한 번에 선택할 수 있어요!
