Uniquely identifying in-world containers

I’m working on serializing persistent data between scenes, so I can do stuff like place treasure chests in the editor and have them remain empty if the player loads the scene, loots them, unloads the scene, then reloads it.

The broad strokes of doing this are pretty simple- create some LootContainer class with a unique key and a list of its contents, store that in some kind of database class that persists between scenes, then make that data part of the save file so that player actions persist between sessions. Whenever a scene loads, each container simply asks the database for its contents and configures itself accordingly.

Where I’m undecided is on how I should be generating unique keys. There are going to be potentially dozens of containers in each scene, so naming them by hand is infeasible. What I’ve been toying with right now is the idea of using item category + RoundToInt(transform.position), such that each item would be named something like Wastebasket(15,33,7) or TreasureChest(76,12,15). That works, but it also assumes that those objects are completely static- if I want to extend the logic to something that can move, like the trunk of a garbage truck puttering around the block, my approach completely falls through.

So is there a better solution that I’m missing? This seems like such an obvious problem, I feel really daft for drawing a blank.

Generate a hashcode using the timestamp of their editor creation along with a prefix of the scene name to create a GUID?

What about System.Guid?

string myUniqueKey = name + System.NewGuid();

Hmm, both the hashcode and guid would work, I hadn’t thought of doing it on generation in the editor! For some reason I’d only ever thought about assigning IDs at runtime, thank you for the suggestion! ^^

You could put it right in the Reset() method.

public string key;

void Reset() {
    key = name + System.NewGuid();
}

Oh hey, I didn’t even know about that method, thanks for the suggestion!

In case it’s useful to anyone else, my thing requires a LevelManager mono in every scene, so I also played with the notion of adding a custom button to the inspector that simply runs FindObjectsOfType and iterates through them, assigning each one an id of Type + i. I think I’m going to stick with Tony’s method because I don’t want the possibility of an object changing IDs, but I like the simplicity of just flat out naming them type + number.