{
GameObject peopleGam;
public Text peopleText;
char a;
void Start()
{
peopleGam = this.gameObject;
char ch=(char)('A'+Mathf.RoundToInt(Random.Range(0,26))); //产生随机数 转换为char类型
peopleText.text = ch.ToString();
if (Input.GetKeyDown(a))
{
Destroy(peopleGam);
}
}
首先Random.Range是有int类型参数的重载的,不需要Mathf.RoundToInt去强转。
其次KeyCode枚举中的A-Z键是从97-122,你需要用随机得到的数值+97,然后转为KeyCode。
GameObject peopleGam;
public Text peopleText;
void Start()
{
peopleGam = this.gameObject;
int random = Random.Range(0, 26);
char ch = (char)('A' + random);
peopleText.text = ch.ToString();
int targetKey = random + 97;
if (Input.GetKeyDown((KeyCode)targetKey))
{
Destroy(peopleGam);
}
}
这块监控键盘输入,应该写在Update函数里面
试试强制转换呢
(UnityEngine.KeyCode)a