Search Unity

Unity Hangs When Running a simple script

Discussion in 'Scripting' started by Stanchion, Sep 28, 2016.

  1. Stanchion

    Stanchion

    Joined:
    Sep 30, 2014
    Posts:
    269
    I run this in the editor and it causes my editor to freeze.

    using System;
    using System.Net;
    using System.Net.Sockets;
    using UnityEngine;
    using System.Collections;
    public class IPv6test : MonoBehaviour {

    // Use this for initialization
    void Start () {
    Socket s;
    s = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
    IPAddress ip = IPAddress.Parse("ff15::2");
    s.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership, new IPv6MulticastOption(ip));
    IPEndPoint ipep = new IPEndPoint(IPAddress.IPv6Any, 26000);
    s.Bind(ipep);

    while (true)
    {
    byte[] b = new byte[1024];
    s.Receive(b);
    string str = System.Text.Encoding.ASCII.GetString(b, 0, b.Length);
    Console.WriteLine(str.Trim());
    }
    }

    // Update is called once per frame
    void Update () {

    }
    }
     
  2. BFS-Kyle

    BFS-Kyle

    Joined:
    Jun 12, 2013
    Posts:
    883
    That looks like it definitely should cause your editor to freeze - your Start function has an infinite While loop, which will block the main thread. I don't think this is related to the new scripting preview build.
     
    Vedrit likes this.
  3. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    This is exactly the case.

    Also, please use code tags.
     
  4. Stanchion

    Stanchion

    Joined:
    Sep 30, 2014
    Posts:
    269
  5. BFS-Kyle

    BFS-Kyle

    Joined:
    Jun 12, 2013
    Posts:
    883
    I would suggest moving your post to the appropriate forum, as this is primarily for the experimental build.

    But as a super quick answer: it is correct to hang on a socket.Receive call until it receives data. You cannot simply copy-paste this code example and have something working - what are you receiving from? Have you created a server running on port 26000? Are you connecting to the correct IP address? If you want to skip through to the answer of "don't block the game when i call receive" then use the function BeginReceive instead of Receive.