Unity报错 error CS0246: The type or namespace name ‘UI’ could not be found
在我导入老版本的UnityPackage到2021年1月份的Unity版本的时候,出现了许多警告和error,其中就包括了这样的一个错误: The Type Or Namespace UI could not be found 具体的解决方案如下: 首先,在该错误的开头,加入引用 using UnityEngine.UI; 随后,将UI的前缀去掉;例如: 将*UI.Text变为Text*即可; 例如以下代码即可正确运行:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
[RequireComponent(typeof(Text))]
public class ResourceDisplay : MonoBehaviour {
enum resEnum {Wood, Iron, Stone, Gold, Food, Population }
resEnum resSelect;
private Text txtComp;
void Start () {
txtComp = GetComponent<Text>();
}
void Update () {
switch (resSelect)
{
case resEnum.Wood:
txtComp.text = GVars.qWood.ToString();
break;
case resEnum.Iron:
txtComp.text = GVars.qIron.ToString();
break;
case resEnum.Stone:
txtComp.text = GVars.qStone.ToString();
break;
case resEnum.Gold:
txtComp.text = GVars.qGold.ToString();
break;
case resEnum.Food:
txtComp.text = GVars.qFood.ToString();
break;
case resEnum.Population:
txtComp.text = GVars.qPop.ToString();
break;
}
}
}
|