- Home /
Best way to create runtime variables for Scriptable Objects?,What's the best way to create 'runtime values' for Scriptable Objects?
By runtime variables I mean temporary variables that can be edited on runtime without changing Scriptable Object's original values. Think object states, player HP, etc. My project has it so that you can set scriptable object values through a custom inspector in the editor, and these values would be used and edited on runtime.
I'm honestly stumped on how to do this. I've tried instantiating each SO on start but that created a whole slew of issues like not being able to grab objects by reference and instead having to search for their copy. I've thought of copying every single variable in said object on Awake, but I'm not sure that's viable.
What's the best way to go about this? I've searched long and hard on ways to keep original SO values in runtime but I haven't found any good ways to do it. ,By runtime values I mean variables that temporarily store data at runtime so the original variables of the SO aren't permanently changed even after I close the game. The data gets edited on runtime
What I'm currently doing is straight up instantiating the object and storing that instead, but this causes a whole slew of issues like not being able to reference the object directly and having to search for its copy.
I'm wondering if it's a viable idea to make a copy of EVERY SINGLE VARIABLE and initializing them in Awake(). I'm honestly stumped because all solutions for this involve a lot of effort and messy code.
What's a good way to use SOs where its values change on runtime? I'm looking for any workarounds or ideas in general.
Answer by ShadyProductions · Sep 03, 2020 at 09:35 PM
Sounds like your design is flawed, scriptable objects are data containers. That contain data for your actual game objects.
Etc a Card class with color and type of card as string. (Actual game object)
then you have a Card scriptable object for each card with image or something and type + color
and you loop over these data containers, and create your cards based on them
var cards = new List<Card>();
foreach (var cardData in cardScriptableObjects)
{
var card = new Card(cardData.Color, cardData.CardType, cardData.CardImage);
cards.Add(card);
}
You don't use the scriptable objects as gameobjects.
Your answer
Follow this Question
Related Questions
Make the same serialized class as a ScriptableObject? 1 Answer
Serialize a list of scriptable objects to Json 0 Answers
Scriptable Object's Data Gets Lost After Re-opening Unity!!! 1 Answer
Why do I need to serialize a struct inside a ScriptableObject to save its data? 1 Answer
What are the pros and cons of ScriptableObjects vs. JSON for data files? 2 Answers