Garbled texture from Camera....

I’m using this code to get a texture out of a camera view.

var myCam : Camera; 
var theTexture : Texture;
var targetCube : GameObject;


function Start(){
}

function OnGUI () {
	myCam.targetTexture = new RenderTexture(256,256,24);
	myCam.targetTexture.isPowerOfTwo = true;
	myCam.targetTexture.Create();
	targetCube.renderer.material.mainTexture = myCam.targetTexture;
	GUI.DrawTexture(Rect(40,200,400,100),myCam.targetTexture);
}

The result is a garbled image (that keeps changing over time); I include a screenshot…

How to get a proper texture from my camera ?

2 things:

  1. is the camera set to not overdraw the backbuffer with some fill? in that case such stuff can happen cause the previous content will remain in there.

  2. The code there is meant to go into Start not Ongui (aside of the drawtexture), cause you can skip the dream that you can generate a rendertexture 2-4 times per frame and get away without problems :wink:

2 infos:

  1. don’t understand what you mean by “overdrawing the back buffer”… Clear Flags is set to “don’t clear” and Culling Mask is on “Everything” (also tried with a layer containing the object I wanted to render). I don’t know is this is the info you needed…

  2. was thinking it… :o

  1. Thats exactly what I meant with overdrwaing the backbuffer. Because you don’t clear, you basically leave in whatever there was in the backbuffer before, the only place that stuff (garbage) in there will be overdrawn is where you draw something new which is the model and potentially the thing top right.
    But the stuff on the left there is garbage present in the backbuffer from some previous rendering or data storage, its undefine whats in the backbuffer unless you fill it with content though

You should (must?) never use “don’t clear” unless this is one of multiple cameras where another camera is clearing the whole backdrop and filling it with a color or something else

I’ve tried to set Clear Flags to Solid Color and Skybox (I’ve put one) but the result is still garbled…

Can you post the script in its current state?