Webcam texture crashes unity

I am trying to render my webcam on a texture, for that I am using the following unity code:

WebCamTexture webcamTexture = new WebCamTexture();
        Renderer renderer = GetComponent<Renderer>();
        renderer.material.mainTexture = webcamTexture;
        webcamTexture.Play();

But unity keeps crashing. I have tried it with different unity versions (2022.3.48),(2022.3.45), (2021.3.8)
and different PCs as well, but its the same. I am using windows 11 and have tried to make builds as well but the issue remains.

Can someone help?

Recording2024-10-01103335-ezgif.com-video-to-gif-converter

I have also tried to comment the other lines and just run the first line but it still crashes

WebCamTexture webcamTexture = new WebCamTexture();
//Renderer renderer = GetComponent<Renderer>();
//renderer.material.mainTexture = webcamTexture;
//webcamTexture.Play();