- Home /
How to get data from Scriptable Objects without copy-pasting code
I am trying to create an item system in Unity3d. The system relies on saving items as both Prefabs and Scriptable Objects.
Whenever I want to load data from a Item Scriptable Object, I have to create use Item C# script and attach it to a GameObject. Doing this forces me to copy and paste the same fields as the ones in the Item Scriptable Object.
This is a problem because whenever I update the Item Scriptable Object with a new field, I have to make sure I update the Item C# script with the same field.
I have tried looking into a design pattern that might solve the issue of having to copy and paste fields to be in two script files but have not found anything yet. I have looked into using JSON files to replace Scriptable Objects but since Scriptable Object is built into Unity, I would prefer to use it over JSON.
public class ItemBase : MonoBehaviour
{
[SerializeField] private string itemID;
[SerializeField] private string itemName;
[SerializeField] private string itemFlavorText;
[SerializeField] private float itemLevel;
[SerializeField] private Sprite itemSprite;
#region Getters and Setters
public string ItemID { get => itemID; set => itemID = value; }
public string ItemName { get => itemName; set => itemName = value; }
public string ItemFlavorText { get => itemFlavorText; set => itemFlavorText = value; }
public float ItemLevel { get => itemLevel; set => itemLevel = value; }
public Sprite ItemSprite { get => itemSprite; set => itemSprite = value; }
#endregion
/// <summary>
/// Obtains references from the ItemSO on the ItemPrefab class.
/// </summary>
[ContextMenu("Add References")]
public void AddReferences()
{
ItemSO item = gameObject.GetComponent<ItemPrefab>().Item;
ItemID = item.ItemBase.ItemID;
ItemName = item.ItemBase.ItemName;
ItemFlavorText = item.ItemBase.ItemFlavorText;
ItemLevel = item.ItemBase.ItemLevel;
ItemSprite = item.ItemBase.ItemSprite;
}
}
[System.Serializable]
public class ItemBaseSO
{
[SerializeField] private string itemID;
[SerializeField] private string itemName;
[SerializeField] private string itemFlavorText;
[SerializeField] private float itemLevel;
[SerializeField] private Sprite itemSprite;
#region Getters and Setters
public string ItemID { get => itemID; set => itemID = value; }
public string ItemName { get => itemName; set => itemName = value; }
public string ItemFlavorText { get => itemFlavorText; set => itemFlavorText = value; }
public float ItemLevel { get => itemLevel; set => itemLevel = value; }
public Sprite ItemSprite { get => itemSprite; set => itemSprite = value; }
#endregion
}
Answer by haruna9x · Jan 19, 2019 at 06:35 AM
Basically, scripableobject is a lighter version of prefab. Why do you use both to store the same data. You just need to keep the reference of the scripableobject.
public class ItemBase : MonoBehaviour
{
[SerializeField] private ItemBaseSO data;
}
public class ItemBaseSO :ScriptableObject
{
[SerializeField] private string itemID;
[SerializeField] private string itemName;
[SerializeField] private string itemFlavorText;
[SerializeField] private float itemLevel;
[SerializeField] private Sprite itemSprite;
#region Getters and Setters
public string ItemID { get => itemID; set => itemID = value; }
public string ItemName { get => itemName; set => itemName = value; }
public string ItemFlavorText { get => itemFlavorText; set => itemFlavorText = value; }
public float ItemLevel { get => itemLevel; set => itemLevel = value; }
public Sprite ItemSprite { get => itemSprite; set => itemSprite = value; }
}
Your answer
Follow this Question
Related Questions
Calling OnControllerColliderHit() from script not attached to characterController 0 Answers
Can you compare a Gameobject to a Variable? 2 Answers
Scrolling combat text works on enemy. When I duplicate and have two enemies I get a problem. 1 Answer
ScriptableObject reference resetted after a method is finished. 2 Answers