0
\$\begingroup\$

I have a "A1" scene, and from there, I want to create a new scene "A2" on button click and then I want to load the same scene. Creating a scene can be done with,

Scene NewScene = SceneManager.CreateScene("A2");

But this scene is an empty scene and as it is not added in build settings so I used

EditorSceneManager.LoadSceneInPlayMode

which would allow me to load scene without having it in build settings. Till here it is going well but now how do I add the maincamera in this new scene programmatically? I have tried something like

 public void CreateNewScene()
    {
        Scene NewScene = SceneManager.CreateScene("A2");
        EditorSceneManager.LoadSceneAsyncInPlayMode("A2",new LoadSceneParameters {loadSceneMode = LoadSceneMode.Single});
        LoadCamera();
    }

public void LoadCamera()
    {  
        Camera camera = gameObject.AddComponent<Camera>();
        Instantiate(camera, new Vector3(0, 0, 0), Quaternion.identity);
    }

after the new scene is loaded.

\$\endgroup\$
5
  • \$\begingroup\$ What was the result when you tried that? How did it differ from what you wanted? \$\endgroup\$ – DMGregory Nov 25 '20 at 16:39
  • \$\begingroup\$ @DMGregory I wanted a camera in this new scene but still what I get is "Display1: No camera rendering", Also I have edited my post to show the more detailed code. \$\endgroup\$ – apex Nov 25 '20 at 17:12
  • \$\begingroup\$ Where does your newly-instantiated camera appear in the hierarchy window? Is it being created in the previous scene, not the new one? \$\endgroup\$ – DMGregory Nov 25 '20 at 17:42
  • \$\begingroup\$ I have a Maincamera in scene A1 and then now when I switch to A2, the newly instantiated camera does not appear anywhere. I cannot see it in the hierarchy window.. I think it might be created in the previous scene, but then I have to quit the playmode to get back.. my confusion is how do I insatiate a new camera in the new scene? After going to A2, I do not want anything of A1, just completely a new scene with new camera, new prefabs... \$\endgroup\$ – apex Nov 25 '20 at 18:01
  • \$\begingroup\$ What I assume is, I have not instantiated it in the right place, but then I have no idea what is the correct way to do it? \$\endgroup\$ – apex Nov 25 '20 at 18:32
0
\$\begingroup\$

Maybe look into passing the existing camera between scenes using a DontDestroyOnLoad() script on your main camera.

This will ensure the whole GameObject is preserved.

Example script for your main camera:

void Awake() 
{
    DontDestroyOnLoad(transform.gameObject);
}
\$\endgroup\$
1
  • \$\begingroup\$ That is a nice suggestion, but later on I would like not only the camera but other prefabs that are not in the old scene as well to be programmatically be loaded at runtime in this newly created scene. So if I can load this camera component, I can load other components as well. \$\endgroup\$ – apex Nov 25 '20 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.