C
C#8mo ago
JRL

classes

when i call GetPiece() function it returns null everytime and im not sure why
6 Replies
JRL
JRL8mo ago
Jimmacle
Jimmacle8mo ago
1: don't write get and set methods, C# has properties for this purpose 2: your ResetBoard code has a path that passes null as a piece as for "every time," that's not true so you'll have to be more specific about what code isn't working and what exceptions you're getting as an example of properties, your Spot class should be
public class Spot
{
public Piece Piece { get; set; }
public int X { get; set; }
public int Y { get; set; }

public Spot(int x, int y, int piece)
{
X = x;
Y = y;
Piece = piece;
}
}
public class Spot
{
public Piece Piece { get; set; }
public int X { get; set; }
public int Y { get; set; }

public Spot(int x, int y, int piece)
{
X = x;
Y = y;
Piece = piece;
}
}
JRL
JRL8mo ago
hi thanks i wasnt i aware im new to c# "2: your ResetBoard code has a path that passes null as a piece" what do u mean by this?
Jimmacle
Jimmacle8mo ago
i mean you have a line: boxes[i, j] = new Spot(i, j, null); that is the only place i see null being set as the piece
JRL
JRL8mo ago
even without that if i just have boxes[0, 0] = new Spot(0, 0, new Horse());
Debug.Log(boxes[0, 0].Piece); it still returns null when i try to access the piece nvm im just been stupid it does work thanks for ur help
Jimmacle
Jimmacle8mo ago
pepeok