0

I was writing some Code in Unity, when i had a very strange phenomenon. The variable "newBoard", wich is declared in some code above byte[] newBoard keeps its value after it is being changed in the code:

                        case "1001":
                            Debug.Log("white pawn");
                            // pawn
                            if (i >= 8 && i <= 15 && tiles[i+16] == "0000"&& tiles[i+8] == "0000") {
                                // make two steps if its a start pawn and then make him an en passant pawn
                                newBoard = null;
                                newBoard = movePiece (board, i , i+16);
                                newBoard = makeEnpassant (newBoard, i);
                                allBoards.Add(newBoard);

                                test = newBoard;
                            }
                            if (i < 56 && tiles[i+8] == "0000") {
                                // single steps
                                newBoard = null;
                                newBoard = movePiece (board, i, i+8);
                                
                                allBoards.Add(newBoard);

                                if (test == newBoard) Debug.Log("test");
                            }

Assume that in my case both if statements are being met.

I added the test = newBoard; and if (test == newBoard) Debug.Log("test"); are too see if it is actually the variable that keeps its value after i say newBoard = null;

I really dont know what could be my mistake here so i hope someone smarter than me can help me here.

Thank you!

derHugo
  • 83,094
  • 9
  • 75
  • 115
0q_
  • 74
  • 4
  • Please use the correct tags! Note that [`[unityscript]`](https://stackoverflow.com/tags/unityscript/info) is or better **was** a custom JavaScript flavor-like language used in early Unity versions and is **long deprecated** by now. – derHugo Aug 25 '22 at 14:29

1 Answers1

1

test == newBoard is true because they point to the same object, not because the array has the same value at different points of you program. In c#, arrays are reference types.

Here's a simple example that illustrates a part of your problem:

var a = new byte[]{ 1, 2 };
var b = new byte[]{ 1, 2 };

var c = a; 

Console.WriteLine(a == b); // False - values are the same, but point to a different object
Console.WriteLine(a == c); // True - point to the same object
Console.WriteLine(b == c); // False - values the same but point to a different object

If you need/want to check if arrays contain the same values you will need a helper method that will check the values of all bytes in the arrays.

If you want to grab the state of the board at some point you will need to clone it. Then you will be able to compare the values later. It could be something like:

var test == (byte[]) newBoard.Clone();
   
(...)
if (CheckEqual(test, newBoard)) Debug.Log("Same value!");

You can find ideas for CheckEqual at Testing equality of arrays in C#, for example.

tymtam
  • 31,798
  • 8
  • 86
  • 126