Search Unity

Question Changing scriptableobject setup from another script

Discussion in 'Scripting' started by Alterrion, Jan 18, 2023.

  1. Alterrion

    Alterrion

    Joined:
    Apr 8, 2021
    Posts:
    4
    Hello, so I am trying to set up an RPG card game with scriptable objects. I have a prefab for "Card" which includes a Card Display script below.
    upload_2023-1-18_12-19-9.png
    This script inherits values from a scriptable object CardScript.
    upload_2023-1-18_11-57-5.png
    Based on this scriptable object I have a few setup of cards, which include the info about the art, name etc.
    upload_2023-1-18_11-58-47.png

    Now when I instantiate the Card prefab, I am not able to figure out how to select one of these setups to populate the Card's parameters from inside another script. I can drag and drop the e.g. "Green" setup onto the Card prefab and then it will instantiate it with the "Green" parameters correctly, but let's say I now want to instantiate another card type "Red" when I click on something during runtime, how am I able to change the scriptable object setup from Green to Red in the Card prefab? Basically how to programatically drag and drop a different scriptable object setup onto the Card Display script?

    I tried loading the setups from resources directly, in the CardDisplay script: card = Resources.Load<CardScript>("Assets/Red.asset"); but it instantiated cards with no parameters.even if I drag and dropped something in the editor.

    Or is this a completely wrong way of doing this? I wanted to avoid having to add every single card inside some list.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    This isn't a post for the 2D forum (despite all the tags) but rather a post for the Scripting forum.

    I'll move your post for you.
     
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,938
    Like any Unity object, you can make copies of scriptable objects with
    UnityEngine.Object.Instantiate()
    .

    So you could easily load one of these cards and Instantiate it.

    That said, if I were to make a card game with mutable values (which I assume is why you want to instance them), I would keep all the mutable data in plain C# object wrappers, of which you can freely instance from your scriptable objects without having to worry about mutating your scriptable object assets.
     
  4. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    245
    I have a setup like this for everything in my game. A database of items, enemies, etc using scriptable objects.

    The easiest way to do this is by making a global object on your stage (with “don't destroy on load”) that has a list of all your scriptable card objects. This way you can easily grab them and their variables anytime by accessing that list in game, dont need to instantiate anything.

    to make things even easier:
    https://assetstore.unity.com/packages/tools/utilities/serialized-dictionary-lite-110992

    This free plugin will let you set it up like a dictionary in your global object class, so each card has a name, like this for example.


    My above code has a serialized dictionary using that plugin. You can make something similar with SerializedDictionaryBase<string, CardData> CardData;

    And then access it easily from that gameobject using CardData['green'].Cost (or whatever names you happen to use)
     
    Last edited: Jan 18, 2023
    Eristen likes this.