Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by aclee · Apr 27, 2015 at 06:40 AM · javascriptnetworkingcrash

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image saud_ahmed020 · Apr 27, 2015 at 07:09 AM 0
Share

Can you show me the crash report??

avatar image aclee · Apr 27, 2015 at 07:11 AM 0
Share

I added it to the post @saud_ahmed020

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by HarshadK · Apr 27, 2015 at 07:20 AM

 while(true)

makes your while loop go into infinity which is crashing Unity.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aclee · Apr 27, 2015 at 07:27 AM 0
Share

So why is the one in the update also crashing unity

avatar image HarshadK · Apr 27, 2015 at 07:47 AM 0
Share

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.

avatar image aclee · Apr 27, 2015 at 08:49 AM 0
Share

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

avatar image HarshadK · Apr 27, 2015 at 11:20 AM 0
Share

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.

avatar image Bunny83 · Apr 27, 2015 at 01:27 PM 2
Share

@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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UnityScript struct causes unity to crash 1 Answer

Your "own" structs ... are they indeed passed by value? 1 Answer

Browser interaction to Unity 0 Answers

Javascript / Unityscript adding elements of an array 2 Answers

Play Audio While Button is Held Down 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges