- Home /
How to call Camera.Render from Update
Hi,
From Unity 5.6, I got the following error when Camera.Render() is called in Update() function:
Rendering camera 'DrawSomethingToRenderTexture', but calling code does not set it up as current camera (current camera: 'Main Camera') UnityEngine.Camera:Render()
I noticed that Camera has SetCurrent(Camera) static function, but this function did not help.
Is there any way to remove this error? Or, Camera.Render() should not be called in Update function? I need to call it in Update function to see a rendered texture in Editor. The script has "ExecuteInEditMode" attribute but camera events such as OnPostRender are not called in edit mode. So, I had to manually call Camera.Render() in Update function which will be called in edit mode.
Thanks
Answer by Bunny83 · Apr 04, 2017 at 10:54 PM
Camera.Render directly renders that camera. It should not be called from Update. While the Update methods are called the rendering engine is not "active".
Have a look at the flowchart in the docs. From there you should call Render() highly depends on the reason why you want to render the camera manually.
If you want to render the camera after all enabled cameras has been rendered you may want to use a coroutine with WaitForEndOfFrame or simply render it during the Repaint event in OnGUI.
Thanks for the reply. That makes sense, but I still got the same error.
I would like to call Camera.Render() between user input in edit mode and Scene View rendering though, I tried OnDrawGizmos, OnGUI, and WaitForEndOfFrame. (OnGUI and WaitForEndOfFrame were not called unless Game View is visible)
I feel like it is a bug of Unity now. If the scene has one object that calls Camera.Render, I got this error:
Rendering camera 'DrawSomethingToRenderTexture', but calling code does not set it up as current camera (current camera: '$$anonymous$$ain Camera') UnityEngine.Camera:Render()
And if the scene has two objects, I got this:
Rendering camera 'DrawSomethingToRenderTexture1', but calling code does not set it up as current camera (current camera: 'DrawSomethingToRenderTexture2') UnityEngine.Camera:Render() Rendering camera 'DrawSomethingToRenderTexture2', but calling code does not set it up as current camera (current camera: 'DrawSomethingToRenderTexture1') UnityEngine.Camera:Render()
I don't know why the current camera at the first error is not $$anonymous$$ain Camera but DrawSomethingToRenderTexture2, it seems like the current camera remains after Camera.Render finished.
Your answer
Follow this Question
Related Questions
How can I set the exact camera render size for render textures? 0 Answers
RenderTexture doesn't get cleared and camera just adds to it 1 Answer
Is there a way to write to Deferred Depth from a command buffer in CameraEvent.AfterImageEffects? 1 Answer
How to get depth texture and render texture from one camera 0 Answers