Keeping some info on objet when loading scenes

Hello!

I am in need of some help, I am having a game with different scenes and each scene (map) has lots of nodes I can build stuff on, but when I go back to the same scene I’ve been to I need to building to be loaded as it were when I left it.

I’ve only found some examples of this but nothing that I felt confortably trying with my many nodes in different scenes and I also realise I neecd to save down and load data, that’s about my knowledge of doing this.

Hope someone can help me out here!

Best, Marcus

You do not need to save data to transfer it from scene to scene. The simplest way would be to use a static script; only issue with a static script is you would have to manually reset variables if for say returning to title screen.

I do not?

I have Scene1 which i build 3 buildings on 3 different nodes, then I go to scene2 and build 1 building on a node, then I wanna go back to scene1 and have those 3 buildings still be there.

How can this be done by a static script?

Thanks in advance!

You could use DontDestroyOnLoad. This preserves an object when you load a new scene. Remember though that this means that you always have that object around, even if you don’t need it, unless you destroy it manually.

I think you could also use ScriptableObjects within a session to store data, because these objects exist independently from your scenes. You would store your data in one, load a different scene and once you return to the scene that needs the data, load it from that ScriptableObject. Note that ScriptableObjects do not change between sessions, your changes to them are not persistent between sessions (if you can change them at all at runtime).

Unless you plan to never save the game, you need a save system.
If you can never save the game, you still need to save the data, you just don’t need a system for it.

Save systems in Unity are a real pain in my opinion because there’s no easy way that I know of to identify prefabs, and even if you identify a prefab, no easy way to get it back. There’s also no good way to identify instance data for a prefab. This is the single worst limitation in Unity in my opinion, not only with no good solution, but no good solution even feasible. But maybe someone else has discovered something and can provide a better answer.

What I do in Empire of Ember is give every object either a GuidComponent or if a scriptable object, implement an interface that returns a Guid. The GuidComponent, as its name implies, is a component that just contains a Guid. It manually requires you add this component to every object that can be saved or loaded in the game.

When I save an object I save the Guid value, which is part of what I call a SaveState. A SaveState is actually a token, and contains all data needed to restore the object. I subclass the SaveState with additional information as needed. When a value in an object changes that should be saved, it’s kept in the SaveState subclass rather than the class instance in-game.

In order to get an object back, I have to have a scriptable object with a giant List that I can iterate through and find a game object from the Guid that identifies it. This is broken into parts, so I can search only weapons in the game, for example. This is extremely slow and memory intensive, and that downside shows up in my game. But there’s no other solution that I know of that actually works and is reasonable to use.

Why not use Asset Bundles?

Asset Bundles have to be rebuilt every time you change anything in the bundle. This is not feasable in a game with 100GB + of source assets, as I’d have to wait half an hour every time

Why not use Addressables?

They don’t work if your game uses complex DLLs. I’ve repeatedly brought up this issue for over a a year and I think I’m shadowbanned from the addressables forum because I never get an answer. I even sent a repro case to Unity QA, they agreed the issue existed, then took no action and just closed the case

As Ardenian stated you would use DontDestroyOnLoad. A static script still exists on scene changes so if you create a static script then create/reference your objects using that script and set them to not be destroyed on load they will always carry over.

If you want to save things so you can reload your objects after application relaunched you can use PlayerPrefs in where you will create a string of data that has a representation to whatever you want. Let’s say you want to save an object and you have building types 0,1, and 2. You could create a string “120” and when you read back that string char by char you know ‘1’ is building type 1 and ‘2’ is building type 2, etc. But you also need to save nodes that represent building placement. So now the easiest way for you would be to use delimiters separating sets of data. Let’s say nodes are stored the same way as building types; 0,1, and 2. Your string could read like this “01|12|30|”. Set 1 is “01” node 0 and building type 1 and so on.

Thanks for all the info!

This seems so unnecessarily hard from someone coming from GameMaker where what I want is just an option in your scene. I am thinking of changing my game instead of doing all this for a few buildings I want to stay…

Thank a bunch once again!