- Home /
Question by
treyman · Jan 12, 2021 at 03:31 AM ·
scripting problemscript.
How do I properly create a new prefab with a nested ScriptableObject?
I have a class Item which contains a ScriptableObject ItemObject.
public class Item : MonoBehaviour
{
public ItemObject itemObject;
}
public class ItemObject : ScriptableObject
{
public string itemName;
}
I then have a ResourceObject ScriptableObject which inherits from ItemObject and a Resource class which inherits from Item and contains a ResourceObject
public class ResourceObject : ItemObject
{
public int collectionDifficulty;
}
public class Resource : Item
{
public ResourceObject resourceObject;
}
I am needing to add the Resource to a list of Items
List<Item> inventory = new List<Item>();
I also need to be able to access the collectionDifficulty attribute of the resource.
However, when I create a new Resource GameObject, it requires both an ItemObject and a ResourceObject.
How can I create a new Resource which only contains a ResourceObject yet is also an Item?
help.png
(10.9 kB)
Comment
Best Answer
Answer by CmdrZin · Jan 12, 2021 at 05:09 AM
You could try this in Awake()
ResourceObject resourceObject = ScriptableObject.CreateInstance<ResourceObject>();
or Create a ScriptableObject prefab called ResourceObjectSO. Put it in a Resources folder and use
ResourceObject resourceObject = Resources.Load<ResourceObject>("ResourseObjectSO");
Then resourceObject will be set automatically and you can use
int someInt = resourceObject.collectionDifficulty;