- Home /
unity crashes when script runs
in my simple udp server finder script when i select client it makes unity crash and i can't find why;
the comment code is what makes the crash; when i uncomment the while loop in StartClient function it crashes so at first i though it was the while loop that made it crash so i commented it and wrote it again in the update function so when i say that clientstarted is true in startclient() the update function will run the code but that makes it crash also, so i can't realy guess what makes it crash (when i mean crash i mean in windows make the editor not respond and eventually close itself and on android just close the app).
import System.Net.Sockets;
private var udp_server:UdpClient;
private var udp_client:UdpClient;
private var udp_port:int = 18000;
private var udp_broadcast_ip:IPAddress = IPAddress.Parse ("224.0.0.224");
private var udp_received_message:String;
private var udp_endpoint:IPEndPoint;
private var selected:boolean = false;
private var clientStarted:boolean = false;
function StartServer(){
udp_server = new UdpClient(udp_port, AddressFamily.InterNetwork);
udp_server.JoinMulticastGroup(udp_broadcast_ip);
udp_endpoint = new IPEndPoint(udp_broadcast_ip, udp_port);
InvokeRepeating("StartBroadcastUDP", 0.0,0.3);
}
function StartClient(){
udp_client = new UdpClient();
udp_endpoint = new IPEndPoint(IPAddress.Any, udp_port);
udp_client.Client.Bind(udp_endpoint);
udp_client.JoinMulticastGroup(udp_broadcast_ip);
/*
while(true){
yield;
var udp_received_message_byte:byte[] = udp_client.Receive(udp_endpoint);
udp_received_message = Encoding.Unicode.GetString(udp_received_message_byte);
print("Received Message: " + udp_received_message);
}*/
clientStarted = true;
}
function StartBroadcastUDP(){
var udp_broadcast_message = Encoding.Unicode.GetBytes("GAME SERVER");
if(udp_broadcast_message != ""){
udp_server.Send(udp_broadcast_message, udp_broadcast_message.Length);
}
}
function OnGUI(){
if(!selected){
if(GUI.Button(Rect(0, 0, 100, 100), "Server")){
StartServer();
selected = true;
}else if(GUI.Button(Rect(100, 0, 100, 100), "Client")){
StartClient();
selected = true;
}
}
}
function Update(){
/*
if(clientStarted){
var udp_received_message_byte:byte[] = udp_client.Receive(udp_endpoint);
udp_received_message = Encoding.Unicode.GetString(udp_received_message_byte);
print("Received Message: " + udp_received_message);
}*/
}
and if it helps here is the log file http://pastebin.com/JZ1kY62a
Answer by HarshadK · Apr 27, 2015 at 07:20 AM
while(true)
makes your while loop go into infinity which is crashing Unity.
In does not matter where you put this loop. Once execution enters the loop it will go into infinity.
Say when Update is called for first time then while loop is entered and it never finishes crashing Unity.
I ment the if you see i tried it twice, one time in the while loop (in the startclient functoin) and another time in the update function without the while loop
If you look at the UdpClient.Receive $$anonymous$$ethod it states that:
The Receive method will block until a datagram arrives from a remote host.
So your Update is halted till it receives any packet. And since it might not be receiving packet for a long time causing Update to halt the execution, which might be resulting in a crash.
@aclee: There are generally two ways:
Use a seperate thread for your network code. That's the usual approach. $$anonymous$$eep in $$anonymous$$d that Unity's API isn't threadsafe, so make sure you use the Unity API only from the main thread.
Use a non-blocking socket by setting "udp_client.Client.Blocking = false". Im' not sure if the UdpClient supports non-blocking IO, you have to try it yourself. However non blocking socket IO requires you to do a lot checks manually. So you have to check if there's actually data to be read prior calling Receive. Also since you don't know when an operation completes you have to constantly poll / check if there's new data or if an action has completed. Just search for "non blocking udp sockets" and you should find a lot about how to use them.