- Home /
Question by
Steven-1 · Dec 15, 2011 at 10:20 AM ·
camerainstantiatecrashinstantiating
Instantiating camera crashes my pc
Can someone explain to me why this makes my pc crash every time ? (when I press the space key):
public class RenderScreenshot : MonoBehaviour { public int width=6000; public int height=3375;
public string filename= "screen";
private const string directory = "../../Screenshots";
private int index=1;
void Start ()
{
var directoryInfo = new DirectoryInfo(directory);
if (!directoryInfo.Exists)
{
Directory.CreateDirectory(directory);
}
else
{
var fileInfos = directoryInfo.GetFiles(filename + "*.png");
index = fileInfos.Max(fi => System.Convert.ToInt32(fi.Name.Substring(filename.Length, fi.Extension.Length - filename.Length)));
}
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Camera cam = Object.Instantiate(Camera.main) as Camera;
cam.transform.position = Camera.main.transform.position;
cam.transform.rotation = Camera.main.transform.rotation;
var rt = new RenderTexture(width, height, 24);
cam.targetTexture = rt;
Debug.Log("Camera created: "+cam);
/*cam.Render();
RenderTexture.active = rt;
var screenShot = new Texture2D(width, height, TextureFormat.RGB24, false);
screenShot.ReadPixels(new Rect (0, 0, width, height), 0, 0);
RenderTexture.active = null;
DestroyImmediate (rt);
var bytes = screenShot.EncodeToPNG();
File.WriteAllBytes(Path.Combine(directory, filename+index+".png"), bytes);
DestroyImmediate(cam.gameObject);*/
}
}
}
Note that it doesn't crash when I uncomment the part that is commented out.
Comment
Your answer
Follow this Question
Related Questions
aim 3D text at camera 1 Answer
Change position of instantiated parent's children 1 Answer
Get transform of instantiated Prefab; Help appreciated. 1 Answer
Adding a variable to instantiated object causes infinite objects to spawn. 2 Answers
How to make camera position relative to a specific target. 1 Answer