- Home /
Singleton and multiple scene data
Hi, I currently have a singleton in my project called GM.
I also have 3 scenes. I'm now using additive loading for the scenes. In scene 2, I have a manager script with a list that I'd like to access from the GM script (for saving purposes).
What would the best way to go for making this list accessable? I've heard many people say static is not good.
So far I've pinned ideas down to: make the manager script singleton, make it a static, or reference from the GM.
P.s. I don't need the whole script to be accessible, just that one list of bools which i will get from an array of type where my class is a data holder (it has a Gameobject, int and 2 bools).
Thanks for any help
Answer by xxmariofer · Jul 28, 2020 at 09:52 AM
first, making the list static it is totally fine but only when making the list static makes sense, is the list dependant of the instance of the manager itself? or you want that list to get shared acrross all manager instances? if you want the list to get shared is totally fine to make it static. using the singleton pattern is a good option, specially if there will be only one instance of the manager class and only one, but abusing of singletons might impact the code structure later in the project. i imagine GM stands for gamemanager and manager is other type of manager class, if thats the case i would not create a singleton for each manager class but only one singleton (the GM) and reference to those scripts from the GM
Yes, the manager script will only have one instance. But I only need that whole script for that scene. It's only the list that needs to be used outside of the scene. So singleton would still be ok? There's no performance issues for making it a singleton when only needing to access the list outside. The other methods and functions in the manager script are using things that are in the scene 2. Would having a singleton of this manager script cause errors because the scene 2 objects aren't in scene 1 for example but the manager with the scene 2 refs is.
Thanks
if the manager script is only beig used in one scene i would discard singleton, if the instance of the manager only appears in one scene but you need the list in all scenes go with the static approach
Hi, With a static list, it wouldn't lose track or overwrite when I reload the same level. So if I change the static int list then go to scene 2, then change it again and then return to scene 1, it would have the last changed list? Would this also be the same if I had a static instance of the monobehaviour class. Static $$anonymous$$yClass instance=this; Without don't destroy on load, would it behave similarly? Thanks
Your answer
Follow this Question
Related Questions
Data from a simple singleton / static class return null 1 Answer
good way to get reference of transform of newly instantiated player object? 0 Answers
How does a singleton maintain a persistent value? 1 Answer
how to set up interaction method using raycast? 1 Answer
some behavior on over-shoulder view and static var in update() 0 Answers