Unity variables disappear when a method is called on an event from an external process
I'm writing a chess UI with Unity as well as an external engine (executable written in c#). I can send and receive data to and from the process (engine). This is working, But when the Make_Move method is called, when the process responds back with data, a problem occurs. While debugging the code execution just stops when trying to access the Unity objects in the Make_Move method, and The objects are missing namely the piece gameObject and sprite but the rest of the variables(that are not unity objects) is still there. I don't get any errors, all of the variables are part of a class that is kept in an array that keeps track of the piece it's sprite the gameObject as well as other things.
why would only the unity objects disappear from the object array?
why does code execution stop when trying to access the unity objects (sprite's etc)?
How to fix this?
Unity class for sending and receiving a 4 digit string, the start, and end position of the move to make (xyxy)
public static class UCI
{
static Process process = new Process();
static UCI()
{
ProcessStartInfo si = new ProcessStartInfo()
{
FileName = @"C:\Users\hanno\OneDrive\Documents\Coding\Projectsc#\ChessEngine\ChessEngine\bin\Debug\ChessEngine.exe",
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true
};
process.StartInfo = si;
process.OutputDataReceived += new DataReceivedEventHandler(OnRecieved);
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
}
public static void SendLine(string command)
{
Debug.Log("send: "+ command);
process.StandardInput.WriteLine(command);
process.StandardInput.Flush();
}
public static void Kill()
{
process.Kill();
}
private static void OnRecieved(object sender, DataReceivedEventArgs e)
{
string text = e.Data;
Debug.Log("Recieved: " + text);
if(text.Length == 4)
Game.MakeMove(new Move(Board.Squares[(int)char.GetNumericValue(text[0]),(int)char.GetNumericValue(text[1])],Board.Squares[(int)char.GetNumericValue(text[2]), (int)char.GetNumericValue(text[3])]));
}
}
Unity make_Move method where the problem occurs:
static void Make_Move(Move move)
{
var end = move.end.GetPiece();
//posistion is a vector2Int, this line does not cause any trouble
print("Pos: " + Squares[move.start.GetPosition().x, move.start.GetPosition().y].GetPosition());
//this line causes exexution to stop without error. if it is commented out execution will continue.
print("obj: " + Squares[move.start.GetPosition().x, move.start.GetPosition().y].gameObject);
//lines below this is not executed
SetPiece(move.start.gameObject.GetComponent<Image>().sprite, move.end.GetPosition(), end.MoveCnt);
RemovePiece(move.start.GetPosition());
// set the current turn to the other player
CurrentPlayer = CurrentPlayer == players[0] ? CurrentPlayer = players[1] : CurrentPlayer = players[0];
}