I adjust my level settings in a Scriptable Object and I want to access that Scriptable Object from a non-Monobehaviour C# class. When I use ScriptableObject.CreateInstance<> method, I get default values of fields in ScriptableObject; but I want to access the values that I adjusted in the inspector. How can I do that ?
My scriptable object class
[CreateAssetMenu(fileName = "NewLevelSetting", menuName = "Level/New Level Setting")]
public class ScriptableLevelSettings : ScriptableObject
{
// Only for initial level settings
[SerializeField] private int _forwardPlatformCount = 30;
public int ForwardPlatformCount { get => _forwardPlatformCount; }
[SerializeField] private int _bendPlatformCount = 0;
public int BendPlatformCount { get => _bendPlatformCount; }
[SerializeField] private int _forwardPlatformIncreaseAmount = 2;
public int ForwardPlatformIncreaseAmount { get => _forwardPlatformIncreaseAmount; }
[SerializeField] private int _bendPlatformIncreaseAmount = 1;
public int BendPlatformIncreaseAmount { get => _bendPlatformIncreaseAmount; }
}
LevelSettings class should access the scriptableobject to set its initial values and adjust those values with the given "level".
public class LevelSettings
{
private ScriptableLevelSettings _initialSettings;
private int _forwardPlatformCount = 30;
public int ForwardPlatformCount { get => _forwardPlatformCount; private set => _forwardPlatformCount = value; }
private int _bendPlatformCount = 0;
public int BendPlatformCount { get => _bendPlatformCount; private set => _bendPlatformCount = value; }
private int _forwardPlatformIncreaseAmount = 2;
private int _bendPlatformIncreaseAmount = 1;
private int _maxBendPlatformCount = 3;
public LevelSettings(int level)
{
InitializeSettings();
SetSettings(level);
}
private void InitializeSettings()
{
_initialSettings = ScriptableObject.CreateInstance<ScriptableLevelSettings>();
if (_initialSettings != null)
{
_forwardPlatformCount = _initialSettings.ForwardPlatformCount;
_bendPlatformCount = _initialSettings.BendPlatformCount;
_forwardPlatformIncreaseAmount = _initialSettings.ForwardPlatformIncreaseAmount;
_bendPlatformIncreaseAmount = _initialSettings.BendPlatformIncreaseAmount;
}
}
private void SetSettings(int level)
{
_forwardPlatformCount += level * _forwardPlatformIncreaseAmount;
// Uncomment for linear bend count increase
//_bendPlatformCount += Mathf.FloorToInt(level / 15) * _bendPlatformIncreaseAmount;
_bendPlatformCount += Random.Range(0, _maxBendPlatformCount);
}
}
I want my LevelSettings class to access the values below:
