As the title says i want to know how to duplicate a scene through code/script.
In the editor or at runtime? A currently loaded scene?
at runtime and it should be an other scene that isnt currently loaded, sorry for the late reply
like when i press a button it should duplicate the scene through a script
So what do you mean by “Duplicate” the scene, and how is that different from simply loading it?
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
Yes but… in the game at runtime I’m not sure what that actually means. What’s the point of having two duplicate (but not loaded) scenes?
It now sounds like you’re describing an editor script. If that’s the case you should be able to use this: Unity - Scripting API: AssetDatabase.CopyAsset
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
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.
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
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.
ok i get it now, thanks! sorry for bothering you this much…
No bother, I wouldn’t be here if I didn’t want to help people. Good luck!
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 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?
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…
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://discussions.unity.com/t/799896/4
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() on a GameObject instance, or use ScriptableObject.CreateInstance() 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