0
\$\begingroup\$

I want to make a new scene, when my game is running by script that not added additionally to my scene.
Example : I press "R" and a new scene is created.

\$\endgroup\$
  • 1
    \$\begingroup\$ While your game is running in the editor? Scene creation doesn't exist in a non-editor build of the game, so if this is something players need to be able to do then we'll need to find a different solution. Try telling us about what game feature you're hoping to use this scene creation to accomplish, and we may be able to suggest alternative ways to serve that need. \$\endgroup\$ – DMGregory Sep 11 at 20:31
0
\$\begingroup\$

As @DMGregory said there is no scene creation in non-editor mode. So my solution will be something like this:

Create a new scene in the editor and add the new scene to build and then when user press "R" Load newly created scene.

using UnityEngine;
using UnityEngine.SceneManagement;

public class ExampleClass : MonoBehaviour
{
    void Start()
    {
        //Load scene with name
        SceneManager.LoadScene("OtherSceneName", LoadSceneMode.Additive);
        //Load scene with build index
        //SceneManager.LoadScene(1);
    }
}

For more information Read this.

As you have not shared what you want to do in the new scene but i will explain a scenario:

Let suppose you have 100 levels in your game and you want to load level 5 in the scene then you have to create "Prefabs" for all 100 levels. Then when user press "R" load newly created scene and instantiate the level 5 prefab.

using UnityEngine;
public class InstantiationExample : MonoBehaviour 
{
    // Reference to the Levels Prefabs. Drag a Levels Prefab into this field in the Inspector.
    public GameObject[] Levels;

    // This script will simply instantiate the Level 5 when the game starts.
    void Start()
    {
        // Instantiate at position (0, 0, 0) and zero rotation.
        Instantiate(Levels[4], new Vector3(0, 0, 0), Quaternion.identity);
    }
}

For more information about instantiating prefab visit here:

If you need more information then let know.

\$\endgroup\$

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.