Search Unity

Question Custom Class Field not showing Custom Class Objects

Discussion in 'Scripting' started by BlackSabin, Jul 24, 2021.

  1. BlackSabin

    BlackSabin

    Joined:
    Feb 27, 2021
    Posts:
    75
    TLDR; I can't pick scriptable objects I want to instantiate from the Editor.

    So I've got a custom class for Item. Its a ScriptableObject, and its data is serialized (as best as I understand that whole process). I have them put into a database asset to keep things all nice and tidy, code here:

    Code (CSharp):
    1. using UnityEngine;
    2. using Loadstone;
    3. using System;
    4. using System.Collections.Generic;
    5.  
    6. namespace Loadstone{
    7.    
    8.     [CreateAssetMenu(menuName = "Loadstone/Database")]
    9.     public class Database : ScriptableObject
    10.     {
    11.  
    12.         public List<Item> items = new List<Item>();
    13.         public List<EquipSlot> equipSlots = new List<EquipSlot>();
    14.         public List<Rarity> rarities = new List<Rarity>();
    15.         public List<DamageType> damageTypes = new List<DamageType>();
    16.         public List<Resource> resources = new List<Resource>();
    17.        
    18.         public string[] myAspectTypes = new string[4]{ "EquipSlot","Rarity","DamageType","Resource" };
    19.  
    20.     }
    21.  
    22. }
    I need to give my characters some items, so to start with I made a script that should give the player an item when you interact with it. I can't select what item to give it; I can click the field, but when it gives me a list of assets to pick from, there are none available. I'm guessing its because its looking for Item assets when they are all stored in the Database asset. "GiveItem" code here:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace Loadstone{
    4.    
    5.     public class OreGen : Trigger
    6.     {
    7.        
    8.         //[SerializeField]
    9.         //public Resource myOre;
    10.        
    11.         [SerializeField]
    12.         public Item ItemToAdd;
    13.        
    14.         [SerializeField]
    15.         public int HowMany = 1;
    16.        
    17.         public override void OnUse(GameObject activator){
    18.            
    19.             ItemContainer theContainer = activator.GetComponent<ItemContainer>();
    20.            
    21.             if(theContainer){
    22.                 theContainer.qAddItem(ItemToAdd,HowMany);
    23.             }
    24.         }
    25.  
    26.     }
    27.    
    28. }
    The field "ItemToAdd" is the one in question; I need a way to give it an item from the Database, or if there is a better way to manage a ****load of items, I'm all ears.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Seems to me like you have the problem inverted. You can drag any Item SO into the ItemToAdd slot and it should work, and the real issue is making sure your Database SO has references to everything. I'm not entirely sure why you need the database object, as you didn't elaborate, but you can easily populate it dynamically in the editor using the AssetDatabase instead of manually dragging new objects into the lists every time you create them.
     
  3. BlackSabin

    BlackSabin

    Joined:
    Feb 27, 2021
    Posts:
    75
    Thanks for the reply, I was specifically trying to use
    Code (CSharp):
    1. AssetDatabase.AddObjectToAsset(newItem, source);
    in order to manage my assets in one file, so instead of trying to keep track of a thousand and a half items by having them all in a folder, I'd access them all through a database file and design it with all the search and custom functionality that I want. I've since scrapped the idea, and have gone to using individual assets in folders. I've an idea or two for better ways about the issue now.