How to use scriptable objects to change a static variable?

I’ve been searching around for an answer to this, and I feel like I’m missing something really obvious! I’m setting up a shop mechanic in my game where buying an item changes a static int in another script for the amount of the object. I’m using the same script for all objects you can buy, but different objects should change different ints.

I know I could achieve this with a big switch, but I feel like there should be a simpler solution, possibly involving scriptable objects. I’m using the following scriptable object:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(menuName = "Scriptables/Tool")]
public class ToolData : ScriptableObject {

    public int ID;
    public Sprite Icon;
    public int GoldValue;
    // variable to be changed here?
}

and I feel like it should be possible to have another variable indicating the name of the variable that should be changed, so that when I call on it in my shop script:

    private void AddToInventoryAmount(int amount)
    {
        Player.PlayerGold -= ToolData.GoldValue;
        // change correct int here
    }

…I can change the correct int based on the variable indicated in the scriptable object. Does anyone know how this could be done?

Thanks for your help! :slight_smile:

if I understand correctly you want to change the amount of an item you have, which in this case I would recomend using a : dictionary (only if you have a single copy of that type)

its basically the same as a list but instead of finding the index in the list you’d find the key in the dictionary, as an example: List<int> intList = new List<int>(){1,5,9,5,1,8}; print(intList[2]); will print out 9

thats nice if you know where the value you need is but in most cases you don’t so a dictionary can realy help you, example : Dictionary<string, int> intDict = new Dictionary<string, int>(){{"Dog",6},{"Cat",3},{"Bird",12}}; print(intDict["Cat"]) will print out 3.
you can store the dictionary in a ScriptibaleObject but it won’t serialize

if you still want to use lists then you could cycle thro each index at the list and when the one you need is found return it, using LINQ will achieve it faster and easier

you can google Unity LINQ and Unity Dictionary for more details

public void ChangeAmount()
{
AddToInventoryAmount(ToolData.GoldValue);
}

    private void AddToInventoryAmount(int amount)
    {

        // change correct int here
        Player.PlayerGold -= amount;
    }