#How to reference a scriptable object at runtime

1 messages · Page 1 of 1 (latest)

tough spindle
#

I have only been able to drag and drop my scriptable object onto a prefab with public reference (in inspector).

How do I do this at runtime? I tried to follow the docs but having an issue (see below).

As an example:
ScriptableObject archerData = ScriptableObject.CreateInstance(typeof(Archer));
AddToDeck(testDeck, archerData);

Where the testDeck has:
public List<ScriptableObject> scriptableObjs;

Note: Archer exists as a scriptable object, PlaceableData : ScriptableObject (along with Warrior, etc)

faint island
wooden lava
#

do you want it to save that reference or something? what's the issue

tough spindle
#

I want to have a deck with references to units in a list (or queue) so that when they are "drawn" the drawn card can instantiate its UI/prefab with data from that reference.

#

So at runtime, if I want to shuffle the deck or swap out the cards, I want to be able to clear/add to this list of data.
My assumption is I want to have reference to this data but not instantiate a prefab/UI card/unit or whatever until it's needed (i.e. drawn or played).

wooden lava
#

cool, what's the issue though

tough spindle
#

invalid cast error

wooden lava
#

just a sidenote, consider the generic signature for CreateInstance instead

tough spindle
#

Oh I see what I've done. I thought my chain of inheritance led to a scriptable object... it does not.

In that case - how do I take the specific scriptable object, in my case
WarriorSOData.asset

wooden lava
#

anyways, why are you creating new SOs there?

tough spindle
#

I think that's my issue, I'm not trying to

wooden lava
#

if they're existing units that you just add to your deck, you would just have a reference to the existing SO in the list

tough spindle
#

I just want to use the scriptable objects I have, and apply that to the list to reference

wooden lava
#

right, then CreateInstance is not the right path

#

you would need to have a reference of that somewhere to begin with

#

otherwise, how would it know what object you want to use

#

then you'd just pass that reference along to be added to the deck, perhaps through some layers of encapsulation or abstraction

faint island
#

you could drag them all to that testDeck

tough spindle
tough spindle
faint island
wooden lava
#

or whatever is responsible for adding this card to the deck, it would need a reference so it can add the right card

high latch
#

I usually load SOs from Resources.LoadAll()