acquiring images from simulation?

Hi, i have a problem with acquiring images from my simulation. I think the problem is in the script i use to manage this, as it works fine and is fluid until i enable a SaveVideo button. Below i write the script i use for this task:

using UnityEngine;
using System.Collections.Generic;
public class CamerasManager : MonoBehaviour {
public bool enableVideoSave;
public string folder = “AcquisitionVideo”;
public int frameRate = 25;
public List cameras = new List();

// initialisation
void Start() {

  if(enableVideoSave) {

     Time.captureFramerate = frameRate;
     if(cameras.Count > 0) {

        // create one main folder which will contain a sub folder for each camera 
        folder = folder + System.DateTime.Now.ToString("_yyyy-MM-dd_HH-mm-ss");
        if(System.IO.Directory.Exists(folder)) {
           System.IO.Directory.Delete(folder, true);
        }
        System.IO.Directory.CreateDirectory(folder);

        // create sub folder for each camera
        for(int i = 0; i < cameras.Count; ++i) {
           if(cameras*.Camera) {*

cameras*.Folder = string.Format(“{0}/{1}{2}x{3}{4}fps”,
folder,
_cameras.Name,_
_cameras.Camera.rect.width,_
_cameras.Camera.rect.height,
frameRate);_

_System.IO.Directory.CreateDirectory(cameras.Folder);
}
}
}
}
}
//function called after Update()
void LateUpdate() {
if(enableVideoSave) {
// for each camera*

for(int i = 0; i < cameras.Count; ++i) {
if(cameras*.Camera) {*
RenderTexture rt = new RenderTexture((int) cameras*.Camera.rect.width, (int)*
cameras*.Camera.rect.height, 24);*
cameras*.Camera.targetTexture = rt;*
rt.antiAliasing = 8;
Texture2D screenShot = new Texture2D(rt.width, rt.height, TextureFormat.RGB24, false);
cameras*.Camera.Render();*
RenderTexture.active = rt;
screenShot.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
cameras*.Camera.targetTexture = null;*
RenderTexture.active = null;
Destroy(rt);_

byte[] bytes = screenShot.EncodeToPNG();
string filename = string.Format(“{0}/frame_{1:D04}.png”, cameras*.Folder, Time.frameCount);
System.IO.File.WriteAllBytes(filename, bytes);
_}
}
}
}
}
[System.Serializable]
public class CameraType {
public string Name;
public Camera Camera;
public string Folder;
public CameraType(string _Name) {
Name = _Name;
}
}
I also attach a screen with the errors. [103907-screenshot.png_
|103907]

Thanks in advance_

_*

You don’t destroy your “screenShot” textures you create in that loop. So you create a new texture every iteration and finally run out of memory.