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!