- Home /
UI on cameras spawn during runtime
I am making a game in which all the characters (robots) are going to be completely modular, meaning that every body part is going to be removable, including all cameras. This also means that my game doesn't have anything on the start; it only has one GameObject "player", with a script for adding of the prefab based objects into the scene. The thing is, the UI elements don't seem to render on cameras spawned during runtime. Is there a way I can make the Canvas render on a specific camera via script or some other way?
Answer by Fluffy_Kaeloky · Jan 29, 2019 at 06:46 PM
You can use the Screen Space - Camera of the Canvas of your UI as RenderMode. You then need to set the Camera for the UI in the Render Camera field.
I'm not sure why your UI wouldn't render on your Camera if using Screen Space - Overlay tho when instancing without more infos. Instancing it shouldn't pose a problem.
Following comments, you can edit the Render Camera field using a script : https://docs.unity3d.com/ScriptReference/Canvas-worldCamera.html
I have tried all three given options, none of them worked, which is probably because the camera doesn't exist on the game spawn. I am asking if I can do this by assigning a script to, for example, "player" object that would assign it to a camera with the settings you have just offered
Without more informations, I can't be sure as to why the UI wouldn't render. Firing in the dark, make sure that the UI elements are in a Canvas in Camera space, and that it's layers and it's children from the canvas are in the UI layer. $$anonymous$$ake sure the camera renders the UI layer.
the screen space - camera requires a camera assigned to it, which it doesn't exist on the game start. please, re-read my question