確定有錯誤,我解決了,謝謝你們幫助,但現在統一繼續擠壓當我按下運行按鈕很多人說因爲循環,但我真的不知道如何修復它統一保持崩潰
GameManager.cd
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections.Generic;
public class GameManager : MonoBehaviour {
public Sprite[] cardFace;
public Sprite cardBack;
public GameObject[] cards;
public Text macthText;
private bool _init = false;
private int _matches = 13;
// Update is called once per frame
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>().setupGrapgics();
if (!_init)
_init = true;
}
public Sprite getCardBack(){
return cardBack;
}
public Sprite getCardface(int i){
return cardFace [i-1];
}
public 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--;
macthText.text = "Number of Macthes: " + _matches;
if(_matches == 0)
SceneManager.LoadScene("Menu");
}
for(int i = 0; i < c.Count; i++){
cards [c [i]].GetComponent<Card>().State = x;
cards [c [i]].GetComponent<Card>().falseCheck();
}
}
}
card.cd
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
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 = 0;
manager = GameObject.FindGameObjectWithTag ("Manager");
}
public void setupGrapgics() {
cardBack = manager.GetComponent<GameManager>().getCardBack();
cardFace = manager.GetComponent<GameManager>().getCardface (cardValue);
flipCard();
}
public void flipCard() {
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;
}
}
在先進的感謝
請閱讀[問],參加[旅遊],當然[MCVE] – Plutonix
你的意思是「崩潰」? –