Unity Crashes when running this code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpotRotate : MonoBehaviour {
private float x;
private bool rotateX;
private float rotationSpeed;

void Start()
{
    x = 90.0f;
    rotateX = true;
    rotationSpeed = 10.0f;

}

// Update is called once per frame
void Update()
{
    while (rotateX = true)
    {
        x += Time.deltaTime * rotationSpeed;

        if (x > 140)
            rotateX = false;
    }

    while (rotateX = false)
    {
        x += Time.deltaTime * -rotationSpeed;

        if (x < 50)
            rotateX = true;
    }
}

}

I’m trying to make a simple spotlight that rotates back and forth from 140 degrees to 50 degrees, and then back again, and I have a very simple bit of code. Should go without saying I’m very very new to programming. ( I don’t know why some of that is in a box and not all of it is)

In your while conditions you have assignment instead of comparison. You should just take out the while conditions altogether and just do if(rotate == true){//Code} else {//Code}

private void Update()
{
   if(rotateX)
   {
        x+= Time.deltaTime * rotationSpeed;
        if(x > 140)
        {
            rotateX = false;
        }
   }
   else
   {
         x += Time.deltaTime * -rotationSpeed;
         if (x < 50)
         {
             rotateX = true;
         }
   }
}

Because you used the While method, in the Update function, it makes Unity wait forever until the statement is true. The while method doesn’t let the next thing happen until it is true, thus, crashing unity because it is never true, so it is stuck there forever, in a loop of checking if the statement in true.

private int ex = 2;

void Update()
{
  while(ex == 1)
  {
    
  }
}

Use the if funtion instead.