2012-02-15 50 views
-1

這裏有輕微的C#問題。 我目前正在用Unity編寫一個小平臺遊戲,並且我有一些raycast檢查了碰撞等。現在C#IndexOutOfRangeException錯誤

,我開始清理一下代碼,這些光線投射的結果存儲到一個整數數組,但我發現了一個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; 

在此先感謝。

+0

'sideRays'正在以'開始初始化()',但我看不出它是不斷運行。是否有可能'sideRays'永遠不會被初始化? – 2012-02-15 19:31:15

+0

在拋出異常之前,Debug.Log(sideRays [counter])是什麼讀取的? – zeroef 2012-02-15 19:31:20

+0

它在Update中運行,它是一個被稱爲每幀的統一函數。 Debug.Log顯示「0」。 – Baburo 2012-02-16 06:00:02

回答

5

我認爲這就是問題所在:

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

你基本上做的兩倍,這意味着迭代,而你的數組初始化是這樣的:

sideRays = new int[8]; 

你可能很想:

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

(順便說一句, d 非常強烈建議您不要使用公共字段,但是這是一個不同的問題。)

+0

由於決定在編輯器中序列化/顯示它們,所以公共字段在Unity代碼中使用太多。示例代碼中沒有使用[SerializeField]屬性,就像它應該那樣。 – Jessy 2012-02-15 21:52:32

+0

謝謝,這是一個經典的錯誤。我只是爲了調試而使用公共字段,我可以在運行遊戲時看到Unity編輯器中的值。儘管感謝您的領導。 – Baburo 2012-02-16 06:01:17

1

您的大小爲8申報sideRays,但它從你的循環快速瀏覽該計數器可以得到儘可能看起來高達10

1

檢查for(int rayHeight = 0;rayHeight>=-4;rayHeight-=1,counter++){您的界限最終你會訪問數組5次,每次循環以及您所定義的數組有8個項目。訪問9和10會引發異常。

相關問題