C#IndexOutOfRangeException错误

时间:2012-02-15 19:26:00

标签: c# unity3d indexoutofboundsexception

这里有轻微的c#问题。 我目前正在使用Unity编写一个小型平台游戏,我有一些用于检查碰撞的光线投射等。

现在,我开始通过将那些光线投射的结果存储到整数数组中来清理代码,但是我得到了一个IndexOutOfRangeException。

我曾尝试多次阅读我的代码,但似乎无法找到导致问题的原因。如果有人可以帮助我,我会很高兴。

这是我的代码:

using UnityEngine;
using System.Collections;

public class PlayerRayCaster : MonoBehaviour {

    public float playerHeight = 1;
    public enum FeetState {Air, Ground};

    public FeetState playerFeetState = FeetState.Air;
    public int feetHitRays;
    public int behindHitRay;

    //Arrays of rays. value of 1 means that ray hits a target, 0 means that it does not hit.
    public int[] sideRays; // [0-3] = Left side. [4-8] = Right side.
    public int[] depthRays; // [0] = Away from camera. [1] = Towards camera.
    public int[] feetRays;

    public int counter;

    void Start(){
        sideRays = new int[8];
        depthRays = new int[2];
        feetRays = new int[3];

    }
    // Update is called once per frame
    void Update () {
        FeetRays();
        SideRays();
        BehindRay();
    }

    //Rays, which check if the character is bumping into an object, left or right.
    void SideRays(){

        float rayLength = 0.4f;
        counter = 0;

        //Left side rays.
        for(int rayHeight = 0 ; rayHeight>=-4 ; rayHeight-=1 , counter++){
            Debug.Log(sideRays[counter]);
            if(Physics.Raycast(transform.position-new Vector3(0,rayHeight/2,0), new Vector3(-1,0,0),rayLength)){
                sideRays[counter] = 1;
                Debug.DrawRay(transform.position-new Vector3(0,rayHeight/2,0), new Vector3(-rayLength,0,0), Color.green);
            }
            else{
                sideRays[counter] = 0;
                Debug.DrawRay(transform.position-new Vector3(0,rayHeight/2,0), new Vector3(-rayLength,0,0), Color.yellow);
            }
        }

        //Right side rays.
        for(int rayHeight = 0;rayHeight>=-4;rayHeight-=1,counter++){
            if(Physics.Raycast(transform.position-new Vector3(0,rayHeight/2,0), new Vector3(1,0,0),rayLength)){
                sideRays[counter] = 1;
                Debug.DrawRay(transform.position-new Vector3(0,rayHeight/2,0), new Vector3(rayLength,0,0), Color.green);
            }
            else{
                sideRays[counter] = 0;
                Debug.DrawRay(transform.position-new Vector3(0,rayHeight/2,0), new Vector3(rayLength,0,0), Color.yellow);
            }
        }
    }

    //Three rays, which check if the characters feet are on the ground or not.
    void FeetRays(){
        feetHitRays = 0;
        float rayLength = 0.2f;
        //Shoots three rays down from player.
        for(float i=-0.7f;i<=0.7f;i+=0.7f){

            if(Physics.Raycast(transform.position-new Vector3(i/2,0,0), new Vector3(0,-1,0),rayLength)){
                feetHitRays++;
                Debug.DrawRay(transform.position-new Vector3(i/2,0,0), new Vector3(0,-rayLength,0), Color.green);
            }
            else{
                Debug.DrawRay(transform.position-new Vector3(i/2,0,0), new Vector3(0,-rayLength,0), Color.red);
            }
        }

        //Sets the feet state.
        if(feetHitRays==0)
        {
            playerFeetState = PlayerRayCaster.FeetState.Air;
        }
        else{
            playerFeetState = PlayerRayCaster.FeetState.Ground;
        }       
    }
    //Shoots a raycast in z-direction from the character, to check for door access.
    void BehindRay(){

        behindHitRay = 0;
        float rayLength = 2;

        if(Physics.Raycast(transform.position, new Vector3(0,0,1), rayLength)){ //Away from camera
            behindHitRay = 1;
            Debug.DrawRay(transform.position, new Vector3(0,0,rayLength), Color.green);
        }
        if(Physics.Raycast(transform.position, new Vector3(0,0,-1), rayLength)){ // Towards camera.
            behindHitRay = -1;
            Debug.DrawRay(transform.position, new Vector3(0,0,-rayLength), Color.green);
        }
    }
}

这是给我例外的一行:

sideRays[counter] = 1;

提前致谢。

3 个答案:

答案 0 :(得分:5)

我认为这是问题:

for(int rayHeight = 0 ; rayHeight>=-4 ; rayHeight-=1 , counter++)

你基本上这样做了两次,这意味着 10 迭代,而你的数组初始化如下:

sideRays = new int[8];

你可能想要:

// Note the > instead of >=
for (int rayHeight = 0; rayHeight > -4; rayHeight--, counter++)

(顺便说一句,我非常强烈地建议您不要使用公共字段,但这是另一回事。)

答案 1 :(得分:1)

你声明了大小为8的sideRays,但是从快速浏览你的循环可以看出,计数器可以高达10。

答案 2 :(得分:1)

检查for(int rayHeight = 0;rayHeight>=-4;rayHeight-=1,counter++){上的边界你最终会为每个循环访问阵列5次,并且你已经定义了你的阵列有8个项目。访问9和10将引发异常。