Communication with ESP32 / Arduino

Hello all I, hope this is the correct place for this question. If something isn’t clear please let me know and I’ll see if I can clarify it. I’ve been working on this for so long that it all seems logical to me.

I’m trying to communicate with an arduino I want my unity game to request information from the arduino and the arduino to then send back information (coordinates extracted from a physical game with sensors) that unity then will use to display that.
But for now I simplified it as much as possible to just communication between the 2 and can’t even get that to properly work yet.

Unity sends a text command over serial to the arduino “TEST” the arduino then replies with “Received TEST” and “TEST REPLY” but unity seems to only register “TEST” and not “TEST REPLY” while it does send both if I test it in the arduino IDE serial monitor.

Unity code:

using System.Collections;
using System.IO.Ports;
using UnityEngine;

public class SerialCommunication : MonoBehaviour
{
    private SerialPort serialPort;

    void Start()
    {
        string portName = "COM6"; // Set to COM6
        int baudRate = 115200; // Set to 115200 baud rate
        serialPort = new SerialPort(portName, baudRate);
        serialPort.ReadTimeout = 500; // Set a read timeout in milliseconds
        serialPort.WriteTimeout = 500; // Set a write timeout in milliseconds

        try
        {
            serialPort.Open(); // Open the serial port
            Debug.Log("Serial port opened successfully");
        }
        catch (System.Exception e)
        {
            Debug.LogError("Failed to open serial port: " + e.Message);
        }

        StartCoroutine(ReadFromArduino());
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Debug.Log("Space key pressed");
            SendDataToArduino("TEST"); // Send "TEST" to trigger the reply
        }
    }

    void SendDataToArduino(string message)
    {
        if (serialPort.IsOpen)
        {
            try
            {
                serialPort.WriteLine(message); // Send data to Arduino
                Debug.Log("Sent: " + message);
            }
            catch (System.Exception e)
            {
                Debug.LogError("Error writing to serial port: " + e.Message);
            }
        }
        else
        {
            Debug.LogWarning("Serial port is not open");
        }
    }

    IEnumerator ReadFromArduino()
    {
        while (serialPort.IsOpen)
        {
            try
            {
                string receivedData = serialPort.ReadExisting(); // Read all available data
                if (!string.IsNullOrEmpty(receivedData))
                {
                    Debug.Log("Received from Arduino: " + receivedData);
                    if (receivedData.Contains("TEST REPLY"))
                    {
                        Debug.Log("Received: TEST REPLY");
                    }
                }
            }
            catch (System.TimeoutException)
            {
                // Handle timeout exception if needed
            }
            catch (System.Exception e)
            {
                Debug.LogError("Error reading from serial port: " + e.Message);
            }
            yield return null;
        }
    }

    void OnApplicationQuit()
    {
        if (serialPort != null && serialPort.IsOpen)
        {
            serialPort.Close(); // Close the serial port when the application quits
            Debug.Log("Serial port closed");
        }
    }
}

Arduino code:

void setup() {
    Serial.begin(115200); // Initialize serial communication at a baud rate of 115200
    while (!Serial) {
        ; // Wait for serial port to connect. Needed for native USB port only
    }
}

void loop() {
    if (Serial.available() > 0) {
        String receivedText = Serial.readStringUntil('\n'); // Read the incoming text
        Serial.print("Received: ");
        Serial.println(receivedText); // Print the received text
        if (receivedText == "TEST") {
            Serial.print("TEST2: ");
            Serial.println("TEST REPLY"); // Send "TEST_REPLY" if the received text is "TEST"
        }
    }
}

I’m probably just overlooking something really obvious but not sure what to search for to help with this question.