- Home /
Edit Render Camera programmatically
I´m loading a prefab from the Resource folder. This prefab has a Canvas with the property Screen Space - Camera. I want to add from script the Render Camera once this component is loaded (so in start function). The camera object that I want to add is already on the scene.
How to do it? Something like FindByTag?
Answer by FlaSh-G · Jun 21, 2017 at 08:41 AM
If it's the main camera, you can access it using Camera.main
. If it's not, usually a public/serialized field comes in handy:
public Camera canvasCamera;
public Canvas canvasPrefab;
public void CreateDatCanvas()
{
var instantiatedCanvas = Instantiate(canvasPrefab);
instantiatedCanvas.worldCamera = canvasCamera;
}
Hi FlaSh-G,
I already load the prefab on the scene, what I want to do is to assign the Render Texture to something that is not in the same prefab (the camera in the scene). It seems that I can´t drag and drop the camera to the public canvasCamera because is not in the same prefab
The code I posted is supposed to be on the object that handles the spawning, as indicated by the Instantiate line. This object naturally needs to be in the scene prior to spawning the canvas (or else it wouldn't be there to spawn the canvas...). It can thus hold a the reference to the camera.
So basically the spawner for the canvas knows the references that the spawned canvas needs to know and gives it to it after spawning. Like a person introducing "the new guy" to the important people in the room (scene) that he needs to know, after letting him in.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to take screenshot of Unity camera every single frame? 1 Answer
Mouse movement object in a straight line 2 Answers
When is current camera null? 2 Answers