- Home /
String[,] returns wrong value in recursion
Hi there, I seem to be having a strange issue with recursion and my 2D array. Here's a simplified version of my code that gives the same issues.
puzzle[,] is a string[15,15] with filled values. Calling any form of puzzle[x,y] returns the correct value. The loop below always prints out the first value found, e.g. if I call PrintItem(3), it will always print out the value found in puzzle[3,1] despite Debug.log(x) will print 3, then 4, then 5, etc...
void PrintItem(int x){
Debug.Log(puzzle[x,1]);
Debug.log(x);
x++;
if(x < 10){
PrintItem(x);
}
}
geting any puzzle[x,y] outside this function returns the correct value. but in this loop the debug of puzzle[x,y] always returns the same value, even though just loggin x will print out incrementing values.
The array itself is solid, looping through all items outputs the correct values in each cell.
Any thoughts?
edit: replaced example with an even simpler example
edit: using a jagged array produces same results
Can you show the actual puzzle declaration, how you fill puzzle, and the actual PrintItem function? Or even better a fully working snippet of code that demonstrates the problem. There's too many unknowns with what you have provided, plus the code as shown isn't valid... puzzle declaration is just a comment, Debug.log (lowercase 'l') doesn't exist etc.
The comment under the snippet says that everything prints out correctly if I call puzzle[3,1], puzzle[1,5] or any other variation of puzzle[x,y]. I'll remove those 'declarations' from the snippet as to not confuse anyone else.
Your answer
Follow this Question
Related Questions
Longest Common Subsequence Recursive Function Error. 1 Answer
replacing a character in a String 1 Answer
returning a variable in javascript 1 Answer