Issue with using modulus operator with array

I created a for loop that increases and adjusts the placement (offset) of a group of GameObjects so that they sit flat on a horizontal plane. The loop uses the variable “stackcount” to increment the game objects vertically. This variable will increase throughout the game.

I have tried different versions of this loop. At best, I can get if to cycle through multiple times but I can not get it to run through a remainder, just a full loop with all the game objects. I’m trying to use the remainder operator (%) to get the loop to run through a final time with the remaining balance of the “stackcount” variable. I keep getting the following error in Unity; “IndexOutOfRangeException: Array index is out of range.”

Any suggestions?

        if (loop1count >= arrsize && stackcount > 0)

        {
            NewArrayIndex = ((NewArrayIndex % blockarray.Length) + blockarray.Length) / blockarray.Length;    

            for (NewArrayIndex = 0; NewArrayIndex <= stackcount; NewArrayIndex++)  

                    {
                    
                    blockarray[NewArrayIndex].transform.localScale = Vector3.Scale(blockarray[NewArrayIndex].transform.localScale, new Vector3(1f, blockgrowth, 1f));

                        objectcount++;
                        stackcount--;
                        loop2count++;
                        Debug.Log(loop2count.ToString());

                    blockarray[NewArrayIndex].transform.position = blockarray[NewArrayIndex].transform.position + new Vector3(0, 1, 0);  //offset adjustment

            }

        }

Ok so there is a lot going on here, and I can point out some issues although I’m still unsure exactly what you are trying to accomplish here.
**

  1. NewArrayIndex = ((NewArrayIndex % blockarray.Length) + blockarray.Length) / blockarray.Length; I have no idea what the purpose of this line is. It’s the only place you use the % operator, but I can guarantee this line is not doing what you think it is. (The right side will resolve to some number between 1 and 2 no matter what numbers you plug in). On top of this, in the next line you immediately overwrite whatever value NewArrayIndex held (the for loop sets it to 0).
    **
  2. In the for loop you are setting NewArrayIndex to values from 0 to stackcount, but inside the for loop you are also decrementing stackcount. I’m pretty sure this is not what you intended. If your starting value for stackcount is 100, then it will only loop until NewArrayIndex hits 50 because stackcount is also dropping every loop.
    **
  3. Your error is happening because NewArrayIndex is greater than the length of the blockarray. If you want to get rid of this error, you could call blockarray[NewArrayIndex % blockarray.Length]instead.
    **
    If you can explain very precisely what you are trying to do, and maybe give an example of what stack count is and how big the blockarray is and what the resulting placement of blocks should be I could help you organize your code so that it does exactly what you want in a cleaner manner. I just don’t fully understand what you are trying to do with this code.