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
}
}
Answer by unity_ek98vnTRplGj8Q · Jan 31, 2020 at 04:19 PM
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.
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).
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.
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.
Thanks for the reply and yes, I have quite a few errors!! The Code you referenced in #1 was to address the "wrapping issue" that you provided a suggestion for in #3 - I'll try that. The loop was not allowing for the remainder.
A little background... I'm trying to create a for fun project where I calculate compound interest and then visualize by generating stacks of money. These are single game objects that grow vs separate blocks that take up too much GPU time in AR. The program will move through years and display a visual representation of the compound growth. I take the total value of the funds calculated in a given year and divide by $10,000 to get the stackcount variable. As you move through the years, these stacks grow in size vertically. I have an array of 10 game objects that represent each "stack" of money. I'm trying to cycle through the array and increase the size each game object through vertical growth to represent the addition of a new money "stack". As you can imagine, stackcount does not always divide evenly into the array. I seem to have the most trouble in getting a value less than the array to "wrap" back through.
**I believe I do want to decrease the value stackcount every loop to control the amount of "stacks" generated.
Thanks again!
Ok, just so I understand correctly - you have 10 blocks. Together these represent a sum of money. You want to represent compound interest by growing each stack (you could have chosen to place additional blocks on top of the existing ones to represent new money but you have chosen to optimize and scale the block size ins$$anonymous$$d). The stack count variable is based off of the amount of new money for the year. You want to take this stack count and grow the blocks one at a time, with each growth representing and additional $10,000. You have 10 blocks, so for example $150,000 dollars would equate to a stack count of 15, so you want to grow all 10 blocks then cycle through and grow the first 5 blocks again. Is this correct?
Exactly! I tried individual blocks but with greater sums, it would cause performance issues.