Error to set UNITY as client with MATLAB as server

Hi everybody,

I find hard time to design a taylor-made solution for my project, even with some time spending on tutorials and forums, so here is my post !

I’m totally beginner with UNITY and C# coding but not with MATLAB. My goal is to use values computed in MATLAB to set the main color of an object in UNITY. Meanwhile, the color of this object fade in and fade out from main color to black. For the moment :

  • I’ve modified a UNITY script found on forums so “fade in and fade” out routine is working, based on one given color.

  • Now, I would like to update this given color based on value given from MATLAB (say 1 = blue ; 2 = red and 3 = green). I’ve found a script to set MATLAB as server and UNITY as client (see below, source code here : how unity can read data from Matlab socket - Stack Overflow). However 1) there is still an error in the UNITY code that I’m not able to fixe (line writeSocket(“yah!! it works”); ), and 2) I don’t understand where my “MATLAB value” is read / stored / updated so I can use it to update my color variable in the “fade in fade out” code.

Any help will be appreciated, thanks guys and have a nice day !

/ SET UNITY AS CLIENT
using UnityEngine;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System;
using System.IO;

public class Socket : MonoBehaviour {

// Use this for initialization

internal Boolean socketReady = false;
TcpClient mySocket;
NetworkStream theStream;
StreamWriter theWriter;
StreamReader theReader;
String Host = “localhost”;
Int32 Port = 55000;

void Start () {

setupSocket ();
Debug.Log (“socket is set up”);
}

// Update is called once per frame
void Update () {

}

public void setupSocket() {
try {

mySocket = new TcpClient(Host, Port);
theStream = mySocket.GetStream();
theWriter = new StreamWriter(theStream);
socketReady = true;
writeSocket(“yah!! it works”);
Debug.Log (“socket is sent”);

}
catch (Exception e) {
Debug.Log("Socket error: " + e);
}
}
}

writeSocket isn’t a function if it is a function in matlab you would want to access it through mySocket

Thanks for your reply @BubyMB . Any idea about the 2nd question ? Is “TheStream” variable looks like the variable where streamed data could be stored / updated ?