Can I programmatically generate and instantiate a prefab from inside a class, instead of having my class always be a component on the prefab?
It seems like Unity expects everything to be a subcomponent to something in the game hierarchy.
As an example, if I want to create a dozen “items” the player might find in treasure chests, I need to create a prefab and add that item’s class as a component. This really becomes a problem for any code that passes Item(s) around - it has to actually pass GameObjects around.
If a user “picks up” an item, I used to use a method like this:
public void addItem(Item item) { … }
But with Unity, I can’t pass the Item without losing access to the sprite/prefab, so I have to get verbose:
public void addItem(GameObject object) {
var item = object.GetComponent();
}
This just seems a lot of extra work. I don’t want to create a prefab for every single item, I don’t want everything my code to be based on GameObjects.
I hope I’m missing something simple or there are some better Unity-specific patterns I should use.