- Home /
Method variables change value all at once (2020.1.2f1)
Hi, so I have a grid composed by nodes, each of these nodes has a script with a method used to calculate the grid coordinates of a neighbor, the problem is that both of the out variables return the same value, the last one that was set to one of them. Here's the code:
//These are the node cords in the grid, I've checked these and they're fine
int x;
int y;
public void GetNeighborNode(int dir, out int neighborX, out int neighborY)
{
switch (dir)
{
case 1:
neighborX = x + 1;
neighborY = y + 1;
break;
case 2:
neighborX = x;
neighborY = y + 1;
break;
case 3:
neighborX = x - 1;
neighborY = y + 1;
break;
case 4:
neighborX = x + 1;
neighborY = y;
break;
case 6:
neighborX = x - 1;
neighborY = y;
break;
case 7:
neighborX = x + 1;
neighborY = y - 1;
break;
case 8:
neighborX = x;
neighborY = y - 1;
break;
case 9:
neighborX = x - 1;
neighborY = y - 1;
break;
default:
neighborX = -1;
neighborY = -1;
break;
}
Could this be a bug or am I missing something?
Seeing the script that is calling this would be helpful. It is possible that both outs return the same value in every single case depending on what you are passing the function. Also, x and y are never given values so they always equate to 0 as an int cannot be null, so I assume that might be your issue
Hey there, as I said in the comment, the x and y variables have the correct value, they're set by another script, here you have the line of the script calling the function: obj.GetComponent<Node>().GetNeighborNode(dir * 2, out NextNodeX, out NextNodeX);
(There aren't any more lines interacting with the script), and the variables in the calling script aren't neither the problem since I've done some debugging in the node script and the values were sent and saved correctly, the problem, is that the both of the variables return the same value, although they were set different ones.
Ok, the x and y variables are private in the script you have provided, so I don’t know how you are changing them from another script unless you modified this script before posting it. Also, where are you setting the dir value before you pass it to the function? And I just noticed that you are passing NextNodeX, NextNodeX.. I think one might need to be NextNodeY
Answer by ericelbracista · Aug 23, 2020 at 10:11 PM
After some researching, it seems that it was caused due to that I was receiving the two values in the same variable. Although the debugging I made was done in the script calculating the cords, so I can't understand how, but fixing the reception error fixed the calculation error.