Unity说我的Array索引超出了范围?

时间:2016-12-04 21:45:52

标签: javascript arrays unity3d

所以我试图在Unity中创建一个老虎机,我收到一条错误,说我的数组索引超出了范围。这是错误的图片。 如果有人能帮助我搞清楚,我会很感激。

$.get()

}

#pragma strict

var id: int = -1; 
var GO: GameObject;
var mScript: master; // script

// 0=stopped 1, 2, 3, 4=spinning 5=finished
var stateWheel: int = 0;

var angleStart = 0.0;
var angleStop = 0.0;
var angT = 0.0;
var ang = 0.0;
var r = 0.0;
var angleRandom = 0.0;
var countFast: int;
var index: int = -1;

var totalFaster = 0.0;
var totalSlower = 0.0;


var t = 0.0;
var td = 0.0;

function Start (){
    GO = GameObject.Find("Master");
    mScript = GO.GetComponent (master);

    stateWheel = 0;
    angleStart = 0.0;
    angT = 0.0;
    t = 0.0;
} 

function Update () {    
     td = Time.deltaTime; // time since last Update
     t += td;

     switch (stateWheel) {
         case 0:
             initialize();
             return;
         case 1:
             rotateFaster();
             return;
        case 2:
             rotateFast();
             return;
        case 3:
             rotateSlower();
             return;
        case 4:
             rotateReverse();
             return;
        case 5:
             finished();
             return;
     }
 }

 function initialize() {

if (mScript.stateWheels[id] != 1) return;
mScript.stateWheels[id] = 0;

r = mScript.randomNo();
angleStop = 18.0*Mathf.RoundToInt(360.0*r/18.0);
if (angleStop >= 360) angleStop -= 360;
angleRandom = angleStop - angleStart;
if (angleRandom < 0) angleRandom += 360;

ang = 0.0;
stateWheel++;
 }

 function rotateFaster() {
     ang += 0.02;
     if (ang > 10) {
         var angleAdjust = (360 - 335) + (360 - 135);

         var a = angleAdjust + angleRandom;
         if (a >= 360) a -= 360;
         countFast = Mathf.RoundToInt(a/10) + 1;
         stateWheel++;
         return;
    }

    totalFaster += ang;
    if (totalFaster >= 360) totalFaster -= 360;
    angT += ang;
    if (angT >= 360) angT -= 360;
    transform.rotation = Quaternion.Euler(-angT, 0.0, 0.0);
}

function rotateFast() {
     countFast--;
     if (countFast == 0) stateWheel++;

     ang = 10;
    angT += ang;
     if (angT >= 360) angT -= 360;
transform.rotation = Quaternion.Euler(-angT, 0.0, 0.0);

1 个答案:

答案 0 :(得分:0)

正如许多人在评论中所说,问题显然是因为你在行var id: int = -1; id 设置为 -1 :数组起始索引为0。

当您尝试获取不存在的数组元素(数组为null,索引&lt; 0或索引&gt; =数组的长度)时,您将收到此错误。