How can I change the viewport rect of two active cameras at runtime? - Splitscreen Multiplayer

I’m able to change the main camera’s viewport at runtime, and the script is finding Cam2, but I can’t adjust its rect.

The commented out line is the offender.

This script is currently attached to the main camera.

Thanks!

public class P1Cam : MonoBehaviour {
	
	GameObject Cam2;

	void Start(){
		Cam2 = GameObject.Find ("Cam2");
		if (Cam2) {
			Debug.Log ("found Cam2");
			//Cam2.rect = new Rect (0.5f, 0, 0.5f, 0);
			Camera.main.rect = new Rect (0, 0, 0.5f, 1);
		} else {
			Camera.main.rect = new Rect (0, 0, 1, 1);
		}

	}
}

Remember to accept helpful answers

1 Answer

1

Bah, 10 minutes later I got it.

public class P1Cam : MonoBehaviour {
	
	private Camera Cam2;

	void Start(){
		Cam2 = GameObject.Find ("Cam2").GetComponent<Camera> ();
		if (Cam2) {
			Debug.Log ("found Cam2");
			Cam2.rect = new Rect (0.5f, 0, 0.5f, 1);
			Camera.main.rect = new Rect (0, 0, 0.5f, 1);
		} else {
			Camera.main.rect = new Rect (0, 0, 1, 1);
		}

	}
}