memorize objects or actions at scene change

good morning everyone, i was thinking of a way to save the objects and actions performed at the change of scene.
i have already prepared the script for the scene change, nothing complicated for now, very simple. a collider, which loads the new scene when contact is made.
i wanted to understand the way that when you go back to the previous scene, the collectibles (for example the collected coins), or the enemies killed do not appear when loading the new scene.
can you direct me to the best way to do this. i have seen the playerprefs, but i don’t know if it is the best solution.
Thank you

I’ll move your post to the scripting forum as it doesn’t relate to 2D features.

Any type of inter-scene “memory” is typically handled by a GameManager construct. There’s a lot of tutorials on Youtube for the details and concept.

If you understand the concept of a GameManager, here’s two usable examples:

ULTRA-simple static solution to a GameManager:

OR for a more-complex “lives as a MonoBehaviour” solution…

Simple Singleton (UnitySingleton):

Some super-simple Singleton examples to take and modify:

Simple Unity3D Singleton (no predefined data):

Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data:

These are pure-code solutions, do not put anything into any scene, just access it via .Instance!

If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:

public void DestroyThyself()
{
   Destroy(gameObject);
   Instance = null;    // because destroy doesn't happen until end of frame
}

There are also lots of Youtube tutorials on the concepts involved in making a suitable GameManager, which obviously depends a lot on what your game might need.

thank you very much kurt, yes, I have the concept of the game mager in mind and for now I have done something simple. I use it for the management of collectibles, for example coins, where if the character takes them, he accumulates them, and with the change of scene the total. I have no problem with that.

I was thinking about how to keep track of which coin (for example) was taken.
for example scene1 I take the coin, I change scene and the total is 1. up to here ok. from scene2 I go back to scene1, and with the loadscene reload the coin, when once taken it is no longer possible to take it back.

what i thought is that in the gamemanager i kept a list, where at loading (at awake or at start) if that coin is in my list, i destroy it.
I don’t know if this is a good idea.

Ah, sounds like loading / saving, aka “persistence.”

Load/Save steps:

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

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:

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.