- Home /
Create a Main Camera through C#
Is it possible to create a main camera through C#. I have deleted the Main Camera Game Object in the Scene. So far I have
GameObject camera;
camera = new GameObject("MainCamera");
camera.AddComponent("Camera");
The camera dosent appear though when pressing play.
Answer by Propagant · Apr 15, 2021 at 10:02 PM
@Mfarban The scene needs some camera to initialize. Keep the existing camera tagged MainCamera in the scene and destroy it right at the startup. After that replace it with the new one. So I would do this
void Awake()
{
Camera cam = Camera.main;
GameObject newCam = new GameObject("newMainCam");
newCam.AddComponent<Camera>();
if(cam) Destroy(cam.gameObject);
}
You may wanna use newCam.tag = "MainCamera";
to make it easier to reference later with Camera.main
.
Answer by Olgo · Jun 25, 2013 at 12:31 AM
You may want to create a camera prefab and instantiate that.
That doesn't fully answer the question though, does it? Is it possible at all without pre-existing editor resources?
Your answer
![](https://koobas.hobune.stream/wayback/20220613104823im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I want to put main camera floating 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Array index help. 1 Answer