Unity socket program doesn't run

Web Technologies Web Development 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating
_x000D_ _x000D_ 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

Posted on 16 Aug 2022, this text provides information on Web Development related to Web Technologies. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago
_x000D_ 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.

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Important Web Technologies Links