簡體   English   中英

Unity中C#的錯誤

[英]Errors on C# in Unity

我有這些我不理解的錯誤試圖修復自己,但我仍在學習團結

失誤

在我的代碼下

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.FindGameObjectsWithTag ("Manager");

    }

    public void setupGrapgics() {

        cardBack = manager.GetComponents<GameManager> ().getCardBack ();
        cardFace = manager.GetComponents<GameManager> ().getCardFace (cardValue);

        flipCard ();

    }

    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;

    }
}

此錯誤是因為“ FindGameObjectsWithTag”(返回[]),更改為“ FindGameObjectWithTag”

第一個錯誤是因為方法GameObject.FindGameObjectsWithTag ("Manager"); 返回一個數組,您嘗試將數組分配給非數組類型。 您應該改用GameObject.FindGameObjectWithTag("Manager"); (請注意,Object中沒有's'),它僅返回與標記匹配的第一個GameObject。

最后兩個錯誤與第一個錯誤有些相關,因為它們也會返回數組。 行管理器manager.GetComponents<GameManager> ()返回組件的數組。 要僅返回給定類型的第一個組件,請使用manager.GetComponent<GameManager>()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM