1

I made a simple game in unity. I want this game to read data from socket as server. File named Socket.cs, creates a server and listens port 8000, if i run this file in visual studio, it works. I want to run this file when the unity game has started. The problem is here, game starts but socket.cs file hasn't been started. Socket.cs is placed on assets directory in unity game.

The code i used for socket server in c#: http://paste.ubuntu.com/16466625/

Could you help me please to start socket.cs file when the game has been started?

Thank you

Burak Kurt
  • 39
  • 1
  • 6

1 Answers1

3

First of all, Unity does not have a Main function like a normal C# program. So putting a code in public static void Main() will not do anything. Unity uses Start() and other simliar functions as it's starting point not Main().

Socket.cs is placed on assets directory in unity game

Simply placing a script in the Asset Folder does not mean it will work. You have to instantiate the script from another Unity script or attach the Script to a GameObject.

Also even if the code worked in Unity, Unity would freeze because the socket code is synchronous instead of asynchronous. It will block while reading or waiting for client to connect.

You should either make it with a Thread or use asynchronous socket.

Here is complete Unity TCP server that works in Unity. It uses Thread.

I suggest you learn how Unity works. Learn the basic.

Community
  • 1
  • 1
Programmer
  • 121,791
  • 22
  • 236
  • 328