This is a game where basically every scene is a room with doors to other rooms. I’m trying to come up with an elegant way to describe features about a room that I can access from code. Pieces of information that don’t necessarily belong to any of my existing GameObjects. Specifically what brought this issue to mind is background music for a room. Instead of a special doorway that triggers new music, I’d prefer that transitioning rooms itself triggers a check to see what music should be playing. So the room has to know what its own BGM is so the audio manager can query it. Then if I decide later I want different music, I can quickly change that property. But I can see further use cases for things like:
- Defining the subarea of the room in order to change certain visual effects
- A switch for whether the room is “dark” and requires a lantern
- That room’s “position” in the world map
Basically, I’m trying to think of these pieces of information as properties of the “room” or “scene” asset, but I’m not sure how to best connect the information and the room.
I could create a “RoomInformation” prefab in the hierarchy, make a scene template, and a script with that information as a MonoBehaviour. But this is really just a struct in concept; it doesn’t deserve a whole MonoBehaviour or a GameObject. I’d have to search for and find the object when a scene loads (or use a static reference to the current scene’s copy), and you wouldn’t be able to check the properties of any other room except the one you’re in.
I could create a struct and a dictionary within the game state and then populate it from a config file, but that’s probably too far removed from the actual scene itself. It’s not obvious except through documentation or domain knowledge that properties of a room are dictated by some text file somewhere. It also means referring to things like audio clips by name instead of serialized fields.
I could create ScriptableObjects which conceptually represent rooms and use them as wrappers to refer to scenes when transitioning via doors or teleports. This feels closest to what I want to do, but the hierarchical relationship seems to be going the wrong way. The properties of a scene keep a reference to the scene, but the scene itself has no “ownership” of those properties. And, this reference is only a string, which means misspellings and filename changes can cause issues. Though I suppose that’s always the case with scenes in Unity.
To be clear, this is not a question about keeping information/music alive between scenes. I’m looking for the best solution for storing information relevant to a scene as a whole, but not any object within it in particular.