Unity中C#的错误 [英] Errors on C# in Unity
问题描述
我有一些我不理解的错误,试图修复自己,但我仍在学习团结
I have these Error That I'm not understanding tried to fixed my self but I'm still learning unity
在我有密码的情况下
Card.cs
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;
}
}
推荐答案
第一个错误是因为方法GameObject.FindGameObjectsWithTag ("Manager");
返回一个数组,并且您尝试将数组分配给非数组类型.相反,您应该使用GameObject.FindGameObjectWithTag("Manager");
(注意,Object中没有's'),它仅返回与该标签匹配的第一个GameObject.
The first error is because the method GameObject.FindGameObjectsWithTag ("Manager");
returns an array and you attempt to assign the array to a non-array type. You should instead use GameObject.FindGameObjectWithTag("Manager");
(note, no 's' in Object), which returns only the first GameObject that matches the tag.
最后两个错误与第一个错误有些相关,因为它们也会返回数组.第manager.GetComponents<GameManager> ()
行返回组件数组.要仅返回给定类型的第一个组件,请使用manager.GetComponent<GameManager>()
.
The last two errors are related somewhat to the first, since they also return arrays. The line manager.GetComponents<GameManager> ()
return arrays of components. To only return the first component of the given type, use manager.GetComponent<GameManager>()
.
这篇关于Unity中C#的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!