• 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 Captaion · Sep 05, 2019 at 08:16 PM · networkingcrashcrashingcrashesudp

UdpClient.receive crashes unity

Been toying around with networking and I created a simple console application that can send messages over the internet but every time I've tried to bring what I made in my application always crashes unity.

 using System.Collections;
 using System.Collections.Generic;
 using System;
 using System.Net;
 using System.Net.Sockets;
 using System.IO;
 using System.Text;
 using UnityEngine;
 
 public class networkHost : MonoBehaviour
 {
     public int port;
     public IPEndPoint ipep;
     public IPEndPoint sender;
     public UdpClient newSock;
 
     byte[] receiveData;
 
     void Start()
     {
         receiveData = new byte[1024];
 
         ipep = new IPEndPoint(IPAddress.Any, port);
         newSock = new UdpClient(ipep);
         sender = new IPEndPoint(IPAddress.Any, 0);
     }
 
     void Update()
     {
         if (Input.GetKeyDown("j"))
         {
             ReceiveData();
         }
     }
 
     public void ReceiveData()
     {
         //This line crashes unity
         receiveData = newSock.Receive(ref sender);
 
         if (Encoding.ASCII.GetString(receiveData, 0, receiveData.Length) == "jump")
         {
             Jump(100);
         }
     }
 
     public void Jump(int force)
     {
         GetComponent<Rigidbody>().AddForce(Vector3.up * force);
     }
 }
 

I really don't know why it crashes, especially because it exists and works almost identically in a console application. Some help would be appreciated the next thing I was gonna look at was threading but I really don't know.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Captaion · Sep 06, 2019 at 06:39 PM

Figured it out did more reading, udpclient.receive blocks until it receives a datagram which in unity's case means it crashes, so I put it inside an if check, that works. This is what I have now.

 public class networkHost : MonoBehaviour
 {
     public string ip;
     public IPAddress hostaddress;
 
     public int port;
     public IPEndPoint ipep;
     public IPEndPoint sender;
     public UdpClient newSock;
     public Thread networkThread;
 
     public byte[] receivedData;
     public string dataString;
 
 
     void Start()
     {
         networkThread = new Thread(ReceiveData);
         networkThread.Start();
 
         receivedData = new byte[0];
 
         ipep = new IPEndPoint(IPAddress.Any, port);
         newSock = new UdpClient(ipep);
         sender = new IPEndPoint(IPAddress.Any, 0);
     }
 
     void Update()
     {
         ReceiveData();
     }
 
     public void ReceiveData()
     {
         if (newSock.Available > 0)
         {
             receivedData = newSock.Receive(ref sender);
             dataString = Encoding.ASCII.GetString(receivedData);
 
             if (Encoding.ASCII.GetString(receivedData) == "jump")
             {
                 Jump(1000);
             }
         }
         else
         {
             print("No data to receive");
         }
     }
 
     public void Jump(int force)
     {
         GetComponent<Rigidbody>().AddForce(Vector3.up * force);
     }
 }
 

This code as it is now, works for me, I use a basic application outside of unity to send "jump" to my game. Although one thing I noticed for me was that the in editor player would not receive anything through the internet it had to be sent through LAN, and I honestly have no idea why, if anyone knows, cheers.

Comment
Add comment · Show 1 · 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 thelghome · Dec 01, 2019 at 04:03 PM 0
Share

You may probably need WebSocket Solution ins$$anonymous$$d of UDP.

One of example: https://youtu.be/82_-a7WF3vs

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

162 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 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 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 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 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 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 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 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 avatar image avatar image

Related Questions

Game no reopen before go to Desktop or something (FullScreen) 0 Answers

Unity Editor Crashes Upon Startup 0 Answers

Unity crashing constantly 2 Answers

Unity editor editor keeps on crashing on startup 0 Answers

Unity crashes at anything (almost literally) 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