Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved How to duplicate a scene through code

Discussion in 'Editor & General Support' started by UlicniGangster, Aug 2, 2020.

  1. UlicniGangster

    UlicniGangster

    Joined:
    Aug 1, 2020
    Posts:
    9
    As the title says i want to know how to duplicate a scene through code/script.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    In the editor or at runtime? A currently loaded scene?
     
  3. UlicniGangster

    UlicniGangster

    Joined:
    Aug 1, 2020
    Posts:
    9
    at runtime and it should be an other scene that isnt currently loaded, sorry for the late reply
     
  4. UlicniGangster

    UlicniGangster

    Joined:
    Aug 1, 2020
    Posts:
    9
    like when i press a button it should duplicate the scene through a script
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    So what do you mean by "Duplicate" the scene, and how is that different from simply loading it?
     
  6. UlicniGangster

    UlicniGangster

    Joined:
    Aug 1, 2020
    Posts:
    9
    i mean like having a scene and then just duplicating it for example i have Scene1 and i duplicate it so its Scene1(1) but through code like you would do CTRL + D
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
  8. UlicniGangster

    UlicniGangster

    Joined:
    Aug 1, 2020
    Posts:
    9
    i want to make like a starting scene. When you start a new game that scene should duplicate and use that duplicated one, and when the player wants to make a new game it should delete it and duplicate that starting scene and start over again
     
  9. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    That just sounds like reloading the same scene. When you load a scene everything will be back to the way it started in that scene. I'm not seeing where a duplicated scene comes into play here.
     
  10. UlicniGangster

    UlicniGangster

    Joined:
    Aug 1, 2020
    Posts:
    9
    because i want to have a game where you can save your progress i need to have a basic scene where its like the beggining but if i duplicate it scene "Scene" wouldnt be touched but scene "Scene(1)" would be saved where you left off
    sorry if this doesnt make sense because im new to unity, as you can see i joined yesterday
     
  11. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Welcome to Unity.

    That isn't how scenes work. Scenes don't save their changes between play sessions. They are static. Every time you load a scene it is reset to how you had saved it in the editor. Any changes that you want to save, you will have to save that information yourself. For example, any objects whose positions you want to save that are different from that in the static scene - you would have to save that information yourself and reload and reposition those objects to the saved positions when you start the scene via script. Google around for Unity saved game tutorials to learn how to do this.
     
  12. UlicniGangster

    UlicniGangster

    Joined:
    Aug 1, 2020
    Posts:
    9
    ok i get it now, thanks! sorry for bothering you this much..
     
  13. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    No bother, I wouldn't be here if I didn't want to help people. Good luck!
     
    jamespaterson likes this.
  14. graham30

    graham30

    Joined:
    Oct 17, 2019
    Posts:
    5
    May I jump in with a similar question? I am trying to make a game similar to Poly Bridge, and I am stumped as how to save the building state, then run the game (time.timescale = 1) and then go back to the build process to continue editing it? I had tought of copying the hierarchy, loading a blank scene, then pasting it . . but now that I am writing what I am thinking, it's madness :eek: I am struggling with this a bit, can't find any info on doing something like this. My original idea was to save the game state, then reloading it when the bridge fails, would that be the way to go? If so, would anyone have any pointers on that process?
     
  15. DarkryCZ

    DarkryCZ

    Joined:
    Dec 2, 2017
    Posts:
    4
    I realise this is kind of a dead topic by now, but for anyone that gets to this and has the same question:

    This might not be the most effective method, but I would suggest creating a list of all of the joints of the bridge (make a custom object for that) and then save that list via Binary Formatting.
    When you load up the game, you should be able to load the list back up and then you can create instantiated objects based on the data from your list.

    This method is simple for beginners and works on many more things. I used it for storing data like quests, buildings, locations and more...
     
  16. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Let's not do that.

    Do not use the binary formatter/serializer: it is insecure, it cannot be made secure, and it makes debugging very difficult, plus it actually will NOT prevent people from modifying your save data on their computers.

    https://docs.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide

    Load/Save steps:

    https://forum.unity.com/threads/save-system-questions.930366/#post-6087384

    An excellent discussion of loading/saving in Unity3D by Xarbrough:

    https://forum.unity.com/threads/save-system.1232301/#post-7872586

    When loading, you can never re-create a MonoBehaviour or ScriptableObject instance directly from JSON. The reason is they are hybrid C# and native engine objects, and when the JSON package calls
    new
    to make one, it cannot make the native engine portion of the object.

    Instead you must first create the MonoBehaviour using AddComponent<T>() on a GameObject instance, or use ScriptableObject.CreateInstance<T>() to make your SO, then use the appropriate JSON "populate object" call to fill in its public fields.

    If you want to use PlayerPrefs to save your game, it's always better to use a JSON-based wrapper such as this one I forked from a fellow named Brett M Johnson on github:

    https://gist.github.com/kurtdekker/7db0500da01c3eb2a7ac8040198ce7f6