Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7514187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:25:21+00:00 2026-05-30T00:25:21+00:00

Having a slight c# problem here. I’m currently coding a small platformer-game with Unity

  • 0

Having a slight c# problem here.
I’m currently coding a small platformer-game with Unity and I had some raycasts which checked collisions and such.

Now, I started to clean the code a bit, by storing the results of those raycasts into an integer array, but I’m getting a IndexOutOfRangeException.

I’ve tried to read through my code many times but can’t seem to find what is causing the problem. If someone can help me out, I would be really glad.

Here’s my code:

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);
        }
    }
}

This is the line that gives me the exception:

sideRays[counter] = 1;

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:25:22+00:00Added an answer on May 30, 2026 at 12:25 am

    I believe this is the problem:

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

    You’re basically doing that twice, and that means 10 iterations, whereas your array is initialized like this:

    sideRays = new int[8];
    

    You may well want:

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

    (As an aside, I’d very strongly recommend that you don’t use public fields, but that’s a different matter.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am coding this layout as a wordpress theme and having a slight problem
UPDATE : look at the demo here: http://amit-verma.com/template_test/ i am having a slight problem
I'm having some slight problems with fading one div into another, here's my code
I'm having a slight problem with my SDL/Opengl code, specifically, when i try to
I'm having a slight problem that I can't figure out, but should be really
I am having a slight problem. I believe that my Netbeans is set to
I'm working on a simple framework, and I'm having a slight problem. I'd like
Having a slight problem on C#, still quite new to the language but hoping
Having a slight problem with MPMoviePlayerController. I am playing a movie and if the
I am having a slight problem aligning my Horizontal Navigation bar to the center

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.