Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How to get the properties from a Scriptable Object and use them?

Discussion in 'Scripting' started by VileGoo, Sep 19, 2018.

  1. VileGoo

    VileGoo

    Joined:
    Apr 29, 2017
    Posts:
    220
    I'm making an inventory system, and the items you can pick up have a Scriptable Object variable in them where I can place the 'type of item' into it that contains all the properties for the item (name, description, etc). I need to be able get these properties from any Scriptable Object (not just a single one because there could be many different types of items) and use it so I can have a visual inside my inventory UI. How can I do this with what I currently have?

    Code (CSharp):
    1.     public void AddItemToSlot(int itemIndex, ScriptableObject itemToAdd)
    2.     {
    3.         GameObject slotNeeded = GameObject.Find("_Slot" + itemIndex);
    4.         Image slotImage = slotNeeded.transform.Find("SlotSprite").GetComponent<Image>();
    5.  
    6.         slotImage.sprite = itemToAdd.icon; // This is the error, how can I fix this?
    7.  
    8.         Debug.Log(slotNeeded);
    9.     }
    Code (CSharp):
    1.     public void AddItem(ScriptableObject itemToAdd, GameObject interacted)
    2.     {
    3.         // Loop through all inventory slots and get which ones are currently empty and add the item to it
    4.         for (int i = 0; i < InventorySlots.Length; i++)
    5.         {
    6.             if (InventorySlots[i] == null)
    7.             {
    8.                 InventorySlots[i] = itemToAdd;
    9.                 inventoryUI.AddItemToSlot(i, itemToAdd);
    10.                 Destroy(interacted);
    11.                 return;
    12.             }
    13.         }
    14.         Debug.LogError("Inventory Full");
    15.     }
    Code (CSharp):
    1. [CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Generic Item")]
    2. public class Item_Generic : ScriptableObject
    3. {
    4.     new public string name = "New Item";
    5.     public Sprite icon = null;
    6.     [TextArea]
    7.     public string description = "New item description";
    8.     [Tooltip("Must be a unique name. Other scripts will check for this property to determine if it's the correct one, not the name.")]
    9.     public string index = "unique_index";
    10. }

    Edit: "UnityEngine.ScriptableObject does not contain a definition for `icon' and no extension method `icon' of type `UnityEngine.ScriptableObject" This is the error appearing in the InventoryUI script.
     
    Last edited: Sep 19, 2018
  2. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    That is not an error, that is just (presumably) where the error occurred. If you are getting an error, we really need to know precisely what the actual error message is so we can be helpful, otherwise all we can do is blindly guess.
     
  3. VileGoo

    VileGoo

    Joined:
    Apr 29, 2017
    Posts:
    220
    "UnityEngine.ScriptableObject does not contain a definition for `icon' and no extension method `icon' of type `UnityEngine.ScriptableObject" that is the error, i'll add it to the thread for others to see.
     
  4. VileGoo

    VileGoo

    Joined:
    Apr 29, 2017
    Posts:
    220
    @bobisgod234

    If I didn't make it clear enough in the post, here's a bit of clarification: I need to be able to get the properties from any type of Scriptable Object I can assign to an item you can pick up so the inventory UI can display the proper information. So at the moment the Scriptable Object example I gave you is called 'Item_Generic' however there could be another one called 'Item_Locket' for example that requires a unique property the other common items don't need. Both of these Scriptable Objects will have the exact same variables (name, description, icon, etc) so that the inventory UI can use these properties no matter which Scriptable Object it's taking them from. Hopefully this clears up what I need help with.
     
  5. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Item_Locket should inheret from Item_Generic, since Item_Locket must have everything a generic item would have. That means that any locket's you create can be passed as an Item_Generic.

    Code (CSharp):
    1.     public void AddItemToSlot(int itemIndex, Item_Generic itemToAdd)
    2.     {
    3.         GameObject slotNeeded = GameObject.Find("_Slot" + itemIndex);
    4.         Image slotImage = slotNeeded.transform.Find("SlotSprite").GetComponent<Image>();
    5.         slotImage.sprite = itemToAdd.icon;
    6.         Debug.Log(slotNeeded);
    7.     }
    Code (CSharp):
    1.     public void AddItem(Item_Generic itemToAdd, GameObject interacted)
    2.     {
    3.         // Loop through all inventory slots and get which ones are currently empty and add the item to it
    4.         for (int i = 0; i < InventorySlots.Length; i++)
    5.         {
    6.             if (InventorySlots[i] == null)
    7.             {
    8.                 InventorySlots[i] = itemToAdd;
    9.                 inventoryUI.AddItemToSlot(i, itemToAdd);
    10.                 Destroy(interacted);
    11.                 return;
    12.             }
    13.         }
    14.         Debug.LogError("Inventory Full");
    15.     }
    Code (CSharp):
    1. [CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Generic Item")]
    2. public class Item_Generic : ScriptableObject
    3. {
    4.     new public string name = "New Item";
    5.     public Sprite icon = null;
    6.     [TextArea]
    7.     public string description = "New item description";
    8.     [Tooltip("Must be a unique name. Other scripts will check for this property to determine if it's the correct one, not the name.")]
    9.     public string index = "unique_index";
    10. }
    Code (CSharp):
    1. [CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Locket Item")]
    2. public class Item_Locket : Item_Generic
    3. {
    4.     // things unique to Locekt go here.
    5. }
     
    GroZZleR likes this.
  6. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    As well as the help provided by bobisgod234, it sounds like you could use a refresher on inheritance.
     
    bobisgod234 likes this.