【问题标题】:index out of bounds error unity c#索引越界错误统一c#
【发布时间】:2016-08-26 07:15:10
【问题描述】:

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

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

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);
        }
    }
}

【问题讨论】:

  • 哪一行出错了?
  • 什么是稀有的 [roll].componentObjc ?你编程一个类操作符?或者你想写rares[roll].gameObject
  • 错误所在的行是... if (Input.GetKeyDown(KeyCode.Space) || Input.GetTouch(0).phase == TouchPhase.Began) {
  • commons [roll] .componentObjc 或稀有 [roll] .componentObjc。基本上是两个列表。一个包含稀有物品和其他常见物品。在这种情况下,第一个做一个随机列表。然后选择一个列表,它返回以使另一个随机对象进入该列表..对不起,我的英语,但我在谷歌上翻译......

标签: indexing touch screen bounds out


【解决方案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

【讨论】:

    【解决方案2】:

    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 
    
    

    }

    【讨论】:

    • 我做了改动。它的工作方式与以前完全相同。但现在来自同一行的不同错误代码。 "数组索引超出范围"
    猜你喜欢
    • 2015-01-10
    • 2014-06-06
    • 2015-03-22
    • 2013-10-13
    • 2014-01-27
    • 2021-10-11
    • 1970-01-01
    相关资源
    最近更新 更多