索引越界错误统一c#

时间:2016-05-01 12:57:19

标签: indexing touch screen bounds out

以下示例运行良好,但错误索引超出范围。有谁知道我怎么解决?

我的另一个问题是我无法在触摸位置初始化对象。谢谢

False

2 个答案:

答案 0 :(得分:1)

修正。它没有错误

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

答案 1 :(得分:0)

Input.GetTouch(0).phase有错误你应该使用Input.touches[0].phase 将代码替换为代码! 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);
    }
}

}