- Home /
Confused about custom GameObjects,Custom GameObject confusion
Hello! I've been trying to create a custom GameObject for my project but have been hitting a roadblock. The GameObject has a script attatched to it when it's created called InventoryItem. I can create the GameObject just fine from the menu, but once I do I can't drag it into spaces in the inspector that require InventoryItem obejcts.
By this I mean, how you can drag Toggle objects created using the menu into spaces in the inspector that require a Toggle. How do I get Unity to recognize my custom GameObject of BEING an InventoryItem for use in the inspector? thanks!
Here is my code:
[MenuItem("GameObject/Custom/Inventory Item", false, 10)]
static void CreateInventoryItem(MenuCommand menuCommand)
{
//Create the object
GameObject newItem = new GameObject("New Inventory Item");
// Ensure it gets reparented if this was a context click
GameObjectUtility.SetParentAndAlign(newItem, menuCommand.context as GameObject);
// Register the creation in the undo system
Undo.RegisterCreatedObjectUndo(newItem, "Create " + newItem.name);
Selection.activeObject = newItem;
//add components
newItem.AddComponent<InventoryItem>();
}
P.S. No, I can't use ScriptableObjects for this. It's what I used initially, but I've since read that if a ScriptableObject's values are changed often, it's not best to use them because you need to manually save and load each one. I am going to have a lot of InventoryItems with constantly changing values, so I opted to create a ScriptableObject for the "base stats" of an item, add it to my InventoryItem script, and then modify only the values of InventoryItem during runtime. I want them separated for a few other reasons as well, such as having different favorited InventoryItems that all use the same base stats of my ScriptableObject item.
Your answer
Follow this Question
Related Questions
How do I solve a BuildAssetBundles Compilation Error that only occurs in the Editor? 1 Answer
How do I associate my custom object for the serializedProperty objectReferenceValue 1 Answer
Is it possible to write a function in the backend of an editor script for a scriptableObject? 0 Answers
Get callback when assets loading or deserialization finished? 1 Answer
myObjetc..GetType().GetCustomAttributes(typeof(RequireComponent) not working correctly 1 Answer