Почему из-за скриптов?
- Потому что когда я их отключал все нормально запускалось!
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Card : MonoBehaviour
{
public static bool DO_NOT = false;
[SerializeField]
private int _state;
[SerializeField]
private int _cardValue;
[SerializeField]
private bool _initialized = false;
private Sprite _cardBack;
private Sprite _cardFace;
private GameObject _manager;
void Start()
{
_state = 1;
_manager = GameObject.FindGameObjectWithTag("Manager");
}
public void setupGraphics()
{
_cardBack = _manager.GetComponent<GameManager>().getCardBack();
_cardFace = _manager.GetComponent<GameManager>().getCardFace(cardValue);
flipCard();
}
public void flipCard()
{
if (_state == 0)
_state = 1;
else if (_state == 1)
_state = 0;
if (_state == 0 && !DO_NOT)
GetComponent<Image>().sprite = _cardBack;
else if (_state == 1 && !DO_NOT)
GetComponent<Image>().sprite = _cardFace;
}
public int cardValue
{
get { return _cardValue; }
set { _cardValue = value; }
}
public int state
{
get { return _state; }
set { _state = value; }
}
public bool initialized
{
get { return _initialized; }
set { _initialized = value; }
}
public void falseCheck()
{
StartCoroutine(pause());
}
IEnumerator pause()
{
yield return new WaitForSeconds(1);
if (_state == 0)
GetComponent<Image>().sprite = _cardBack;
else if (_state == 1)
GetComponent<Image>().sprite = _cardFace;
DO_NOT = false;
}
}
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Card : MonoBehaviour
{
public static bool DO_NOT = false;
[SerializeField]
private int _state;
[SerializeField]
private int _cardValue;
[SerializeField]
private bool _initialized = false;
private Sprite _cardBack;
private Sprite _cardFace;
private GameObject _manager;
void Start()
{
_state = 1;
_manager = GameObject.FindGameObjectWithTag("Manager");
}
public void setupGraphics()
{
_cardBack = _manager.GetComponent<GameManager>().getCardBack();
_cardFace = _manager.GetComponent<GameManager>().getCardFace(cardValue);
flipCard();
}
public void flipCard()
{
if (_state == 0)
_state = 1;
else if (_state == 1)
_state = 0;
if (_state == 0 && !DO_NOT)
GetComponent<Image>().sprite = _cardBack;
else if (_state == 1 && !DO_NOT)
GetComponent<Image>().sprite = _cardFace;
}
public int cardValue
{
get { return _cardValue; }
set { _cardValue = value; }
}
public int state
{
get { return _state; }
set { _state = value; }
}
public bool initialized
{
get { return _initialized; }
set { _initialized = value; }
}
public void falseCheck()
{
StartCoroutine(pause());
}
IEnumerator pause()
{
yield return new WaitForSeconds(1);
if (_state == 0)
GetComponent<Image>().sprite = _cardBack;
else if (_state == 1)
GetComponent<Image>().sprite = _cardFace;
DO_NOT = false;
}
}
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
public Sprite[] cardFace;
public Sprite cardBack;
public GameObject[] cards;
public Text matchText;
private bool _init = false;
private int _matches = 13;
void Update()
{
if (!_init)
initializeCards();
if (Input.GetMouseButtonUp(0))
checkCards();
}
void initializeCards()
{
for(int id = 0; id < 2; id++)
{
for(int i = 1; i < 14; i++)
{
bool test = false;
int choice = 0;
while (!test)
{
choice = Random.Range(0, cards.Length);
test = !(cards[choice].GetComponent<Card>().initialized);
}
cards[choice].GetComponent<Card>().cardValue = i;
cards[choice].GetComponent<Card>().initialized = true;
}
}
foreach (GameObject c in cards)
c.GetComponent<Card>().setupGraphics();
if (!_init)
_init = true;
}
public Sprite getCardBack()
{
return cardBack;
}
public Sprite getCardFace(int i)
{
return cardFace [i - 1];
}
void checkCards()
{
List<int> c = new List<int>();
for (int i = 0; i < cards.Length; i++)
{
if (cards[i].GetComponent<Card>().state == 1)
c.Add(i);
}
if (c.Count == 2)
cardComparison(c);
}
void cardComparison(List<int> c)
{
Card.DO_NOT = true;
int x = 0;
if (cards[c[0]].GetComponent<Card>().cardValue == cards[c[1]].GetComponent<Card>().cardValue)
{
x = 2;
_matches--;
matchText.text = "Number of Matches: ";
if (_matches == 0)
SceneManager.LoadScene("SimpleScene");
}
for (int i = 0; i < c.Count; i++)
{
cards[c[i]].GetComponent<Card>().state = x;
cards[c[i]].GetComponent<Card>().falseCheck();
}
}
}
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
public Sprite[] cardFace;
public Sprite cardBack;
public GameObject[] cards;
public Text matchText;
private bool _init = false;
private int _matches = 13;
void Update()
{
if (!_init)
initializeCards();
if (Input.GetMouseButtonUp(0))
checkCards();
}
void initializeCards()
{
for(int id = 0; id < 2; id++)
{
for(int i = 1; i < 14; i++)
{
bool test = false;
int choice = 0;
while (!test)
{
choice = Random.Range(0, cards.Length);
test = !(cards[choice].GetComponent<Card>().initialized);
}
cards[choice].GetComponent<Card>().cardValue = i;
cards[choice].GetComponent<Card>().initialized = true;
}
}
foreach (GameObject c in cards)
c.GetComponent<Card>().setupGraphics();
if (!_init)
_init = true;
}
public Sprite getCardBack()
{
return cardBack;
}
public Sprite getCardFace(int i)
{
return cardFace [i - 1];
}
void checkCards()
{
List<int> c = new List<int>();
for (int i = 0; i < cards.Length; i++)
{
if (cards[i].GetComponent<Card>().state == 1)
c.Add(i);
}
if (c.Count == 2)
cardComparison(c);
}
void cardComparison(List<int> c)
{
Card.DO_NOT = true;
int x = 0;
if (cards[c[0]].GetComponent<Card>().cardValue == cards[c[1]].GetComponent<Card>().cardValue)
{
x = 2;
_matches--;
matchText.text = "Number of Matches: ";
if (_matches == 0)
SceneManager.LoadScene("SimpleScene");
}
for (int i = 0; i < c.Count; i++)
{
cards[c[i]].GetComponent<Card>().state = x;
cards[c[i]].GetComponent<Card>().falseCheck();
}
}
}