How to change the display at runtime.

I read this. But simply changing “UnitySelectMonitor” Player Pref only works after restarting the game.

I have arrow buttons to let the user change to the previous or the next available monitor.
Here is the code example for the next monitor button.

    public Resolution[] resolutions;

    public int curRes;
    public int curMonitor = 0;
    public int curWidth, curHeight;
    public bool curFullscreen = true;

public void nextMonitor()
{
        curMonitor++;
        //checking if there is another monitor
        if (curMonitor > Display.displays.Length - 1)
            curMonitor = 0;
        
        //setting PlayerPref
        PlayerPrefs.SetInt("UnitySelectMonitor", curMonitor);
        //calculating available resolutions
        resolutions = Screen.resolutions;
        curRes = resolutions.Length - 1;
        
        //sets the best resolution
        curWidth = resolutions[curRes].width;
        curHeight = resolutions[curRes].height;
        Screen.SetResolution(curWidth, curHeight, curFullscreen);
}

Here are my PlayerSettings:
99305-unbenannt.png

public void ChangeDisplay(Camera camera)
{
Camera cur = Camera.current;
camera.targetDisplay = 0;
cur.targetDisplay = 5;
}

I use this code it is very simple!

It switches cameras displays