[Solved] Editor Freezing When Subtracting From Integer Value Stored In An Array

I may be missing something fundamentally obvious here, but looking elsewhere for solutions has yielded no results, so I thought I’d ask in case someone can enlighten me…

I have a small array of integers created

countTypes = new int[]{0,0,0,0,0,0};

and the value for each index is calculated by counting the number of certain objects in a scene. The counting seems to work fine & stores the correct values, however I’m running into issues subtracting from the stored values and I’m not entirely certain just why.

I’m using a Random.Range to randomly pick one of the stored indexes, and then wish to subtract 1 from the number found there:

rnd = Random.Range(0,countTypes.Length);
countTypes[rnd] = countTypes[rnd] - 1;

Unfortunately, the subtraction line almost always causes the editor to crash. If I comment out that single line, the rest of my code functions without a hitch, so I’m left to assume that I’ve made a mistake in how I’m approaching the math. It seemed simple and straightforward enough, or so I thought, but for whatever reason it simply doesn’t function.

I’ve been trying to debug this for a few days now, believe I have finally narrowed the issue down to this one line of code, cannot understand why it isn’t working properly, and would most certainly appreciate any assistance any of you might have to offer.

Is this code inside a loop of some sort, such as a while loop? If so, can you post the code? If not, how does the code get called?

Hi TonyLi. Yes, the code is being called from within a pair of nested for loops. Let me break down the basics here, without posting up the whole shebang and making everyone’s head spin and eyes hurt, haha :wink:

I’m creating a Shuffle function for a Match 3 style game, so I have a game board that’s a grid. To perform the shuffle, I have the script go through and count each existing piece type, storing those values in the countTypes array. As the pieces are counted, they are removed from the board temporarily until they can be replaced.

Once all of the pieces have been counted along the x and y grid coordinates (36 squares in a 6x6, for instance, starting at 0,0 and ending at 5,5), I iterate back through the grid, beginning at 0,0 and pick a random piece to put in each space if a valid piece can be found.

In order to be considered valid, the countTypes[rnd] value must be above 0, meaning that we still have one or more of those pieces to be used, and, once we’ve reach X(2) and/or Y(2), we check to be sure that the random piece doesn’t match the two pieces behind and/or below it, in order to avoid automatically creating matches. If a valid piece cannot be found, the x & y values are reset to where they were before & the script tries again.

Now that I look at the overlong explanation, I may as well just have posted the 160 or so lines of code, haha. Still, as I said in the first post, everything seems to be fine so long as I do not attempt to subtract one from the stored integer values. If I do include that one line, it almost always chokes on me. Commenting that line out, everything seems to run as it should, though of course it’s not finding & updating the correct values any longer.

Not having seen the code, I’m guessing that this is the culprit. Perhaps it gets into a state where it keeps failing and trying again endlessly.

also you should use countTypes.Length-1 for your highest index

It’s certainly possible, though I’ve done my best to cover those cases as well as I could. I’ve sent the actual script to you via PM and hope it doesn’t make your eyes bleed, haha :wink: Thanks again!

martinmr: Are you sure? I believe that with ints, the Random.Range max is excluded, so for instance Random.Range(0,6) would return a value between 0 and 5, never 6.

ah ok your right for int max value is excluded sry

No worries :slight_smile: I appreciate the suggestion.

TonyLi assisted me a bit further via PM and I’ve got the problem narrowed down further. It looks as though he was correct and there is still a neglected bit of error checking on my part that was leading to an infinite loop. I’m looking it over now and will hopefully have the issue resolved shortly. Thanks again everyone, especially TonyLi :slight_smile: Marking as “solved” preemptively in hopes that I really do find a solution, haha.