1

我在下面的代码中有一个问题:

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。

4

1 回答 1

0

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

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

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

于 2015-11-28T23:17:22.053 回答