Home How to start array at negative indices C#(UNITY)
Reply: 0

How to start array at negative indices C#(UNITY)

user4227
1#
user4227 Published in May 27, 2018, 3:31 am

This question already has an answer here:

  • Array with negative indexes 4 answers

Hi i am very new to c# programming i just want to ask if there's a way to start an index to negative 1?

here's my code

string[,] table = new string[104, 15];
int xIndex = -1;
int yIndex = 0;

if(table.GetLength(0) < xIndex){
    break;
}

if (result.Equals(newPrevious) || result.Equals("A") && yIndex <  table.GetLength(1))
{
    yIndex += 1;
    table[xIndex, yIndex] = result;
}
else
{
    xIndex += 1;
    yIndex = 0;
    table [xIndex, yIndex] = result;
}
previous = result ;

if (!result.Equals("A"))
{
     newPrevious = previous;
}

It is giving me an error here

if (result.Equals(newPrevious) || result.Equals("A") && yIndex <  table.GetLength(1))
{
    yIndex += 1;
    table[xIndex, yIndex] = result;   //<---- ERROR HERE
}

IndexOutOfRangeException: Array index is out of range.

EDIT

I'll explain what i am doing . I am creating a scoreboard for my game and this xIndex and yIndex serves as the position of my object inside of my scoreboard like this

for example i have this value on my string[] strData = {"A ,B ,B ,B ,C ,C ,C ,B ,B ,A ,B ,C ,A ,C "};

it will give me an Exception Error

but if i change my string[] strData to this string[] strData = {"B ,B ,B ,B ,C ,C ,C ,B ,B ,A ,B ,C ,A ,C "};

it will not give me an exception error.

EDIT 2

Here's my whole code

string[,] table = new string[104, 15];
int xIndex = -1;
int yIndex = 0;
string newPrevious = "placeholder";
//C = BLUE, B = RED, A = GREEN
string[] strData = {"A  ,C  ,C  ,C  ,C  ,C  ,C  ,B  ,B  ,A  ,B  ,C  ,A  ,C  "};

//conditions
string[] scoreBoard = new string[]
{"C  ", "B  ", "A  ",
 "C B","B C" };
string OriginalData = "";


void Start(){
    StartCoroutine ("Win_Log");
}

IEnumerator Win_Log(){

    yield return new WaitForEndOfFrame ();

    for (int i = 0; i < strData.Length; i++) {
        OriginalData += strData [i];
        OriginalData += ",";
    }
    string[] newNewData = OriginalData.Split (',');
    string result = "";
    string previous = "";


    foreach (string newStrData in newNewData) {
        Debug.Log ("This is the data : " + newStrData);

        GameObject o = Instantiate (prefab_gameobject) as GameObject;
        o.transform.SetParent (pos_big_road);
        o.transform.localScale = Vector3.one;

        img = (RawImage)o.GetComponent<RawImage> ();

        //check the length so that it won't throw an exception
        if (newStrData.Length > 1) {
            //get only the first letter of the value A,B,C
            result = newStrData.Substring (0, 1);
        } 
        else {
            result = "";
        }

        #region BIG ROAD
        if(table.GetLength(0) < xIndex){
            break;
        }

        if (result.Equals(newPrevious) || result.Equals("A") && yIndex <  table.GetLength(1))
        {
            yIndex += 1;
            table[xIndex, yIndex] = result;
        }
        else if (result.Equals("A"))
        {
            xIndex += 1;
            table[xIndex, yIndex] = result;
        }
        else
        {
            xIndex += 1;
            yIndex = 0;
            table [xIndex, yIndex] = result;
        }
        previous = result ;

        if (!result.Equals("A"))
        {
            newPrevious = previous;
        }

Here's the output i want to expect

EXPECTED OUTPUT

But if change the first string value to B or C its not throwing an exception error.

And as you can see in the image link if the value exceeds from 7 it will move to the x axis but still the same of y axis.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.396773 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO