- Home /
Single Scriptable Object Asset
I'm looking to create a settings file. I'm curious if there is a way to create a single instance of this in the project. I currently have it so that only one can be created and if you try to create it again it just selects the one that has already been created. The only caveat is that you can still duplicate that asset and now you have two. Does anyone have any thoughts?
Answer by ASPePeX · Feb 08, 2017 at 08:29 AM
You could use a static class, accessible from everywhere and no need to attach to anything.
public static class Config
{
public static int Score = 9001;
}
Edit: Ahh, did miss the "file". I do it here (https://github.com/InformatischesQuartett/BlinkenTiles/blob/master/Assets/Scripts/Config.cs) by parsing a json config file. If you want it to be saved from within your game, you could just do it the other way around.
Another possibility would be PlayerPrefs: https://docs.unity3d.com/ScriptReference/PlayerPrefs.html
If you actually want an object holding your info in the scene, check out the Toolbox: http://wiki.unity3d.com/index.php/Toolbox
I see what you're getting at, but I think each provides its own problem/hurdle that I'm trying to avoid if possible. So the issue with the static class is that then a programmer would need to modify these values. I'm trying to expose these settings with a custom inspector so that a designer can easily go in and tweak these values. The issue with Player Prefs is that they don't persist over source control, that's only on a local system, so that won't work in a real project. And toolbox I'm not so sure would work outside of a scene. This settings file is meant to be stored in the project, but yes in a scene, using a custom singleton for monobehaviours is a great option that I've used a few times in the past for various things.
Thank you for the response, I think you're in the ball park, and it was probably on me for not explaining what I was looking for carefully enough.