我正在使用Visual Studio版本Num-1.38.1编译的c#在Unity Engine版本Num-2018.4.9f1中创建2D平台游戏。
Unity Engine的控制台显示以下两个错误,这些错误与我在Visual Studio中拥有的代码一起列出。
错误CS1003:语法错误,预计为'('
错误CS1031:预期类型
我的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof)(Text))]
public class CountdownText : MonoBehaviour {
public delegate void CountdownFinished();
public static event CountdownFinished OnCountdownFinished;
Text countdown;
void OnEnable() {
countdown = GetComponent<Text>();
countdown.text = "3";
StartCoroutine("Countdown");
}
IEnumerator Countdown() {
int count = 3;
for (int i = 0; i < count; i++) {
countdown.text = (count - i).ToString();
yield return new WaitForSeconds(1);
}
OnCountdownFinished();
}
}
答案 0 :(得分:1)
RequireComponent(typeof)(Text))
是错误的。应该是RequireComponent(typeof(Text))
typeof运算符获取类型的System.Type实例。