Why does this cause an infinite loop?

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

public class Camera_Rotation : MonoBehaviour
{
    public float sensitivity = 10.0f;
    public float maxYAngle = 80.0f;
    private Vector2 currentRotation;
    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(1))
        {
            while (Input.GetMouseButtonDown(1))
            {
                Cursor.lockState = CursorLockMode.Locked;
                currentRotation.x += Input.GetAxis("Mouse X") * sensitivity;
                currentRotation.y -= Input.GetAxis("Mouse Y") * sensitivity;
                currentRotation.x = Mathf.Repeat(currentRotation.x, 360);
                currentRotation.y = Mathf.Clamp(currentRotation.y, -maxYAngle, maxYAngle);
                Camera.main.transform.rotation = Quaternion.Euler(currentRotation.y, currentRotation.x, 0);
                if (Input.GetMouseButtonDown(1) == false)
                {
                    break;
                }
            }
        }
        else if (Input.GetMouseButtonDown(1) == false)
        {
                Cursor.lockState = CursorLockMode.None;
        }
    }
}

Unity will lock up 100% of the time EVERY millisecond your scripting code is running.

Nothing will render, no Debug.Log() will come out, no Input will be processed, no GameObjects or transforms will appear to update.

Absolutely NOTHING will happen… until your code either:

  • returns from whatever function it is running

  • yields from whatever coroutine it is running

As long as your code is looping, Unity isn’t going to do even a single frame of change. Nothing.

No exceptions.

Because an input starts true at the beginning of a frame, and remains true until the end of the frame.

So once you enter that loop, it’s never going to exit it as the current frame is never going to end. Your while loop is unnecessary.