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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

that is a very valuable link.
@JoeBlow Thanks. There have been over 4 questions about socket in Unity this week and that link solved all of them.
Thank you so much for help. Awesome explanation :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.