以下示例运行良好,但给出的错误索引超出了范围。有人知道我该怎么修吗?
我的另一个问题是,我无法初始化触摸位置上的对象。谢谢
void Update ()
{
goldDisplay.text = "Gold " + gold;
if (Input.GetKeyDown(KeyCode.Space) || Input.GetTouch(0).phase == TouchPhase.Began) {
int rarityRoll = UnityEngine.Random.Range (0, 100);
if (rarityRoll < 99) {
// choose something from common
int roll = UnityEngine.Random.Range (0, commons.Length);
// instantiate
Instantiate (commons [roll].componentObjc, new Vector3(0,0,1), transform.rotation);
} else {
int roll = UnityEngine.Random.Range (0, rares.Length);
// instantiate
Instantiate (rares [roll].componentObjc, new Vector3(0,0,1), transform.rotation);
}
}
}
修复。它工作时没有错误
void Update ()
{
goldDisplay.text = "Gold " + gold;
for (int i = 0; i < Input.touchCount; i++)
{
Touch touch = Input.GetTouch(i);
if (touch.phase == TouchPhase.Ended && touch.tapCount == 1)
{
Vector3 position = Camera.main.ScreenToWorldPoint(touch.position);
int rarityRoll = UnityEngine.Random.Range (0, 100);
if (rarityRoll < 99) {
// choose something from common
int roll = UnityEngine.Random.Range (0, commons.Length);
// instantiate
Instantiate (commons [roll].componentObjc, new Vector3 (0, 0, 1), transform.rotation);
} else {
int roll = UnityEngine.Random.Range (0, rares.Length);
// instantiate
Instantiate (rares [roll].componentObjc, new Vector3 (0, 0, 1), transform.rotation);
}
}
}
}
tks
Input.GetTouch(0).phase
有错误,您应该使用Input.touches[0].phase
将blow代码替换为您的代码!
void Update ()
{
goldDisplay.text = "Gold " + gold;
if (Input.GetKeyDown(KeyCode.Space) || Input.touches[0].phase == TouchPhase.Began) { int rarityRoll = UnityEngine.Random.Range (0, 100); if (rarityRoll < 99) { // choose something from common int roll = UnityEngine.Random.Range (0, commons.Length); // instantiate Instantiate (commons [roll].componentObjc, new Vector3(0,0,1), transform.rotation); } else { int roll = UnityEngine.Random.Range (0, rares.Length); // instantiate Instantiate (rares [roll].componentObjc, new Vector3(0,0,1), transform.rotation); } }}