Search Unity

Storing multiple unique data and accessing/updating that data across all scenes.

Discussion in 'Scripting' started by RidgeWare, Jun 20, 2018.

  1. RidgeWare

    RidgeWare

    Joined:
    Apr 12, 2018
    Posts:
    67
    Hi, I'm currently at that point during my first project where I need to start working through the concepts of storing/accessing data across different scenes. I've picked quite a complex game in some ways as my first project, but anyway...

    Basically I'm going to have a central game scene (or a hub) where you can view multiple character's stats, and then choose which characters to take into various missions (other scenes). The missions will load the chosen characters at the beginning of each scene, along with all their correct stats, etc.

    If my game had just one player character I could probably use a static class with variables for all the info I need, and just refer to that each scene, but I don't think that would work with multiple characters, all with potentially unique/changeable data.

    I would hugely appreciate any general advice on which direction I should look into. Should I be retrieving/saving to file all the time? Or do I go down the path of dragging all my objects through each scene with Don'tDestroyOnLoad all the time?

    (Loading/saving game state is a whole other issue, but I'd rather deal with that later.)
     
    Last edited: Jun 20, 2018
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Keep it simple and grow it as you need. Don't over-engineer it now. You can't possibly speculate all the ways you'll need it. You have a basic idea of what you need, start there.

    Make a singleton (or a Monobehavior-based singleton if you think your data needs a "pumping" mechanism from Unity's Update() function, or if you want to expose stuff in the Unity editor handily) that spins up when you access it the first time and has endpoints for you to store and retrieve data, whatever "data" is in your context. DONE.

    That gives you a single place to save/load your data in the future. It will also reveal a need for a secondary layer above this basic storage layer to give structure and make access consistent, such as methods for loading/saving each major type of object. But do those only as needed, otherwise you are guaranteed to write a lot of bad code that will constrain and confound your development.