Unity3D не отвечает при включении скрипта.

Unity3D перестает отвечать когда, я включаю скрипт который сделал. Скрипт должен получает данные с порта и вводит их в угол поворота модели по x.

Порт который подключен это порт Arduino.

C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;
using System;
using System.Threading;

public class ArduinoConectServo2 : MonoBehaviour {
    GameObject Player;
    SerialPort Arduino = new SerialPort("COM3", 9600);
    // Use this for initialization
    void Start()
    {
        Thread.Sleep(1000);
    }

    // Update is called once per frame
    void Update()
    {
        Arduino.Open();
        Thread.Sleep(1000);
        int a = 1;
        float angle;
        Thread.Sleep(1000);
        while (a == 1)
        {
            Thread.Sleep(1000);
            angle = Int32.Parse(Arduino.ReadLine());
            Player = GameObject.Find("scelet");
            Player.transform.Rotate(angle, 0, 0);
            Thread.Sleep(1000);
        }
    }
}

Arduino

int x;
int a;
void setup() {
Serial.begin(9600);
x = 471;
}
void loop() {
a = (analogRead(A0) - x) * (-1);
a = a/1.733333333333333;
if(a < 0)
{a = 0;}
Serial.println(a);
delay(30);
}

Unity3D stops responding when I include the script that did. The script should receive data from the port and enter them into the rotation angle of the model in x.

The port that is connected is the port of Arduino.

##C # ##

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.IO.Ports;
 using System;
 using System.Threading;
 
 public class ArduinoConectServo2: MonoBehaviour {
     GameObject Player;
     SerialPort Arduino = new SerialPort ("COM3", 9600);
     // Use this for initialization
     void Start ()
     {

     }
 
     // Update is called once per frame
     void Update ()
     {
         Arduino.Open ();
         int a = 1;
         float angle;
         while (a == 1)
         {
             angle = Int32.Parse (Arduino.ReadLine ());
             Player = GameObject.Find ("scelet");
             Player.transform.Rotate (angle, 0, 0);
             Thread.Sleep (1000);
         }
     }
 }

Arduino##

 int x;
 int a;
 void setup () {
 Serial.begin (9600);
 x = 471;
 }
 void loop () {
 a = (analogRead (A0) - x) * (-1);
 a = a / 1.733333333333333;
 if (a <0)
 {a = 0;}
 Serial.println (a);
 delay (30);
 }

Update() runs on the main thread and so an infinite loop with Sleep(1000) in it will block the main thread forever.
Allow Update() to exit each frame.

First of all, remove all calls to Thread.Sleep(), this should never be done on the main thread, there are always better ways to do this. Unity has a semi asynchronous system that in most cases behaves like a separate thread (although it’s not), called coroutines. Use those instead!

I haven’t tested this, but something like this should work. Try it and let me know if I screwed up somewhere.

using System.Collections;
using System.Collections.Generic;
using System.IO.Ports;
using UnityEngine;
public class ArduinoConectServo2 : MonoBehaviour{

	public GameObject Player;
	SerialPort Arduino = new SerialPort("COM3", 9600);

	private void Start()
	{
		Player = GameObject.Find("scelet"); //Don't run GameObject.Find in every update loop. Doing it once is enough.
        StartCoroutine("ReadArduino");      
	}

	private IEnumerator ReadArduino(){
		Arduino.Open();
		float x = 0.0f;
		while(true){
			//Can't parse a string representation of float to an int. 
			//And since floats are not exact, it's bad practice to compare them with == or !=. 
                    //It's better to see if they are close enough. Change 0.1f if it's too big.
            float angle;
            angle = float.Parse(Arduino.ReadLine());
            

			if (Mathf.Abs(angle - x) > 0.1f)            {
                Player.transform.Rotate(float.Parse(angle), 0, 0);
            }
			x = angle;
			yield return null;
		}
	}

}

This script works as it should:

C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;
using System;
using System.Threading;

public class ArduinoConectServo2 : MonoBehaviour
{
    SerialPort Arduino = new SerialPort("COM3", 9600);
    GameObject Player;
    // Use this for initialization
    void Start()
    {
        Arduino.Open();
    }

    // Update is called once per frame
    void Update()
    {
        int x = 0;
        Player = GameObject.Find("scelet");
            string angle;
            angle = Arduino.ReadLine();
            if (int.Parse(angle) != x)
        {
            Player.transform.rotation = Quaternion.Euler(float.Parse(angle) * -1,-180,0);
        }
            x = int.Parse(angle);
    }
}

Thank you all again for your help.