Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by H1ddeNGames · Jan 19, 2019 at 06:21 AM · scripting problemscriptable objectdesign-patterns

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
     }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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; }
      }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

174 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How to assign variables to a scene (Solved) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges