Unable to print anything in serial monitor when using Unity

I am using Arduino Uno and Unity 3.4.1, and am trying to communicate them with each other. But the serial monitor is not printing anything. In Unity console i am able to see the logged value "Yes". Here is the arduino IDEs code:

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
    if (Serial.available()) {
        String receivedData = Serial.readStringUntil('\n');
        Serial.println(receivedData);
    }
    Serial.println("abs");
}

and the output:

And here is the C sharp script for Unity:

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


public class controller8 : MonoBehaviour
{
    // Start is called before the first frame update
    public string portName = "COM3";
    public int baudRate = 9600;
    private SerialPort serialPort;

    void Start()
    {
        serialPort = new SerialPort(portName, baudRate);
        serialPort.Open();

    }
    // Update is called once per frame
    void Update()
    {
        serialPort.WriteLine("Yes");
        Debug.Log("Yes");
    }
    
}

Here is the output:

Edit : Have to clarify that this code works when i close Unity and run it. And also i am able to send data to Unity using serial print, but not receive from Unity.

You can't use the serial port for multiple applications at the same time. Either you use unity or you use the serial monitor; so close the one that you don't want to use.

//Edit
Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project :wink: See About the Installation & Troubleshooting category.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.