Search Unity

unity crashes when script runs

Discussion in 'Scripting' started by aclee, Apr 27, 2015.

  1. aclee

    aclee

    Joined:
    Oct 5, 2013
    Posts:
    10
    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).

    Code (JavaScript):
    1. import System.Net.Sockets;
    2.  
    3. private var udp_server:UdpClient;
    4. private var udp_client:UdpClient;
    5. private var udp_port:int = 18000;
    6. private var udp_broadcast_ip:IPAddress = IPAddress.Parse ("224.0.0.224");
    7.  
    8. private var udp_received_message:String;
    9. private var udp_endpoint:IPEndPoint;
    10.  
    11. private var selected:boolean = false;
    12. private var clientStarted:boolean = false;
    13.  
    14. function StartServer(){
    15.  
    16.     udp_server = new UdpClient(udp_port, AddressFamily.InterNetwork);
    17.    
    18.     udp_server.JoinMulticastGroup(udp_broadcast_ip);
    19.     udp_endpoint = new IPEndPoint(udp_broadcast_ip, udp_port);
    20.  
    21.     InvokeRepeating("StartBroadcastUDP", 0.0,0.3);
    22. }
    23.  
    24. function StartClient(){
    25.     udp_client = new UdpClient();
    26.  
    27.     udp_endpoint = new IPEndPoint(IPAddress.Any, udp_port);
    28.     udp_client.Client.Bind(udp_endpoint);
    29.  
    30.     udp_client.JoinMulticastGroup(udp_broadcast_ip);
    31.  
    32.     /*
    33.     while(true){
    34.         yield;
    35.         var udp_received_message_byte:byte[] = udp_client.Receive(udp_endpoint);
    36.         udp_received_message = Encoding.Unicode.GetString(udp_received_message_byte);
    37.         print("Received Message: " + udp_received_message);
    38.     }*/
    39.  
    40.     clientStarted = true;
    41.  
    42. }
    43.  
    44. function StartBroadcastUDP(){
    45.     var udp_broadcast_message = Encoding.Unicode.GetBytes("GAME SERVER");
    46.  
    47.     if(udp_broadcast_message != ""){
    48.  
    49.         udp_server.Send(udp_broadcast_message, udp_broadcast_message.Length);
    50.     }
    51. }
    52.  
    53. function OnGUI(){
    54.     if(!selected){
    55.         if(GUI.Button(Rect(0, 0, 100, 100), "Server")){
    56.             StartServer();
    57.             selected = true;
    58.         }else if(GUI.Button(Rect(100, 0, 100, 100), "Client")){
    59.             StartClient();
    60.             selected = true;
    61.         }
    62.     }
    63. }
    64.  
    65. function Update(){
    66.    
    67.     if(clientStarted){
    68.         var udp_received_message_byte:byte[] = udp_client.Receive(udp_endpoint);
    69.         udp_received_message = Encoding.Unicode.GetString(udp_received_message_byte);
    70.         print("Received Message: " + udp_received_message);
    71.     }
    72. }
    and if it helps here is the log file http://pastebin.com/JZ1kY62a
     
  2. vee41

    vee41

    Joined:
    Nov 14, 2013
    Posts:
    32
    Seems that you never exit the while(true) loop, it basically means it will run that loop infinitely and never finish that update cycle (frame won't be rendered). You need some condition to exit the loop.
     
  3. aclee

    aclee

    Joined:
    Oct 5, 2013
    Posts:
    10
    i tried changing the loop to this but it still crashed
    Code (JavaScript):
    1. while(true){
    2.         yield;
    3.         var udp_received_message_byte:byte[] = udp_client.Receive(udp_endpoint);
    4.         udp_received_message = Encoding.Unicode.GetString(udp_received_message_byte);
    5.         print("Received Message: " + udp_received_message);
    6.         if(udp_received_message != null){
    7.             break;
    8.         }
    9.     }
     
  4. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    I would check to see if udp_received_message != null before you access it. Also, do you really need to check this every frame? Also, do you ever set it to false? Now it will run forever, even if it worked.
     
  5. vee41

    vee41

    Joined:
    Nov 14, 2013
    Posts:
    32
    Not really knowing much of how networking and receiving things actually works in Unity this sounds like instead of while loop you might want to check for received messages in update loop. Then the place to do that would be in Update function. But again, don't really know about networking side of things so recommended approach might be different here.
     
  6. aclee

    aclee

    Joined:
    Oct 5, 2013
    Posts:
    10
    it basically stops being null the second it gets a udp message but after some testing i.e erasing the entire loop and just writing var udp_received_message_byte:byte[]= udp_client.Receive(udp_endpoint); still crashes unity

    i did try it in the update function when writing
    1. if(clientStarted){
    2. var udp_received_message_byte:byte[] = udp_client.Receive(udp_endpoint);
    3. udp_received_message = Encoding.Unicode.GetString(udp_received_message_byte);
    4. print("Received Message: " + udp_received_message);
    5. }
    in the update function and instead of the while loop just setting clientstarted to true, but this also crashed unity. could it be that unity just doesn't handle udp well ? (or am i just missing something obvious ?) is there any other way to do lan server lookup without udp?