Screenshot Script for one camera and not another?

When I take a screenshot, it comes from the main camera and not my screenshot camera. Im not sure what I did wrong. Can anyone assist?

Here is the script I used.

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

public class Screenshot : MonoBehaviour
{

    public void SaveDatasheet()
    {
            //ScreenCapture.CaptureScreenshot("GameScreenshot.png");
            StartCoroutine(CoroutineScreenshot());
       
    }

    private IEnumerator CoroutineScreenshot() {
        yield return new WaitForEndOfFrame();

        int width = 1651;
        int height = 2136;
        Texture2D screenshotTexture = new Texture2D(width, height, TextureFormat.ARGB32, false);
        Rect rect = new Rect(1095, 15, width, height);
        screenshotTexture.ReadPixels(rect, 0, 0);
        screenshotTexture.Apply();

        byte[] byteArray = screenshotTexture.EncodeToPNG();

        int filenumber = 0;
        string filepath = Application.dataPath + "/Screenshots/CameraScreenshot" + filenumber.ToString() + ".png";
        int length = 300;
        for (int i = 0; i < length; i++)
        {
            if (!System.IO.File.Exists(filepath))
            {
                System.IO.File.WriteAllBytes(filepath, byteArray);
                Debug.Log("Taking Screenshot " + filepath);
                yield break;
            }
            filenumber++;
            filepath = Application.dataPath + "/Screenshots/CameraScreenshot" + filenumber.ToString() + ".png";
        }

        //System.IO.File.WriteAllBytes(Application.dataPath + "/CameraScreenshot.png", byteArray);
    }


}

The ReadPixels documentation has a good example of taking a screenshot from a specific camera.

BTW - your code is actually taking 300 screenshots.

1 Like