数组超出索引范围

时间:2015-10-06 04:11:55

标签: unity3d unityscript unity5

我在下面的代码中遇到了问题:

var Directions : Vector3[]; //*
function FindNext(){

if(Set.length == 0){
    Debug.Log("We're done.");
    return;
}  

var previous : Transform = Set[0]; 
var pScript : Cell = previous.GetComponent("Cell");

var next : Transform;
var nextScript : Cell;

var prevX : int = pScript.Position.x; 
var prevZ : int = pScript.Position.z; 

var randDirection : Vector3; 
var randSeed : int = Random.Range(0,4);

var nextX : int;
var nextZ : int; 
var counter : int; 

do{ 
    do{ 
        randDirection = Directions[randSeed];
        nextX = prevX+randDirection.x; 
        nextZ = prevZ+randDirection.z; 
        randSeed = (randSeed+1) % 4;
        counter++;
        if(counter > 4){ 
            Set.RemoveAt(0);
            previous.GetComponent.<Renderer>().material.color = Color.black;  
            yield WaitForEndOfFrame();
            return;
        }  
    }while(nextX < 0 || nextZ < 0 || nextX + 1 >= GridSize.x || nextZ + 1 >= GridSize.z); 
    next = GridArr[nextX,nextZ];
    nextScript = next.GetComponent("Cell"); 
    //nextScript.IsOpened = false;
}while(nextScript.IsOpened); 


AddToSet(next); 

DrawDebugLines(10, previous, next);

ClearWalls(previous, next);

yield WaitForEndOfFrame(); 
}

由于某些原因,数组索引存在问题,我真的不知道它是什么。

以下是我得到的错误:

IndexOutOfRangeException: Array index is out of range.
Grid+$FindNext$5+$.MoveNext () (at Assets/Scripts/Grid.js:74)
UnityEngine.MonoBehaviour:StartCoroutine_Auto(IEnumerator)
Grid:Update() (at Assets/Scripts/Grid.js:122)

我在Unity 5中使用UnityScript。

1 个答案:

答案 0 :(得分:0)

嗯,解决方案很简单,就像这个程序在Unity上运行一样,超出范围的阵列是一个带变换的公共阵列,所以要解决问题,唯一需要做的就是&#39 ; s设置数组值:

(0,1)(1,0)(1,1)(1,0)

这个值是因为数组必须在迷宫中包含四个可能的方向。