Hello.
So,a few weeks back,i made a thread asking,to put it shortly,how to make a class that extends a “Base” class,wich in turn extends a ScriptableObject. I was told that Unity had a few problems with making classes selectable from the editor,and that i should post again with more info about the game i’m making; So,here i am.
The game i’m making is a tribute game to a relatively obscure game called “Space Station 13”. Look it up if you don’t know what it is and you wish to know,but here’s the deal: In the game,there are HUNDREDS,and i mean HUNDREDS,of different items that the player can pick up in their hands by clicking on them and manipulate in various other ways. Here’s the deal though: A lot of items have different reactions and perform different tasks when interacted with,E.G.: A burning piece of metal will burn my hand if i try to touch it with my bare hands,an alien artefact might spawn a few hostile mobs if interacted with,and so forth.
Therefore i tried to make a “Base Item” class,wich would allow me to extend all items from it: It had all the functions for all the various cases,being touched,being interacted,being used on another item etc.,it had an “Icon” 2DTexture variable,a description String,and all the variables any item should have.I’d then create an actual Item class extending the “Base Item” class for each and every item,and then override the functions that needed to be changed,like “When this item is picked up,it will make a sound and then be placed in the player’s hand” instead of simply “Being placed in the player’s hand”. This way,i could have a list of type “Base Item” that could act as an inventory where items could be stored,since all items extend the “Base Item” class. Needless to say,this would also be necessary for allowing players to interact with any items,unless i made Ifs for hundreds of items,wich i am clearly not doing. Only problem is,i can’t simply make an object prefab and add an item class as a component to it. This is necessary to make the item prefabs that actually get spawned in the scene when a player puts down an item,or when an item is spawned,or anything else that requires an item to be on the ground in the scene.
I am pretty much desperate for a solution,and i will gladly take any suggestions you may have.
Thanks!
Can you not do this?
public class BaseItem : ScriptableObject
{
///base logic
}
public class ItemWhichBurnsWithFire : BaseItem
{
}
There may be better ways to go about it though… you could have an “item” that’s a gameobject… and attach various behaviours to it that extend its functionality… such as a BurnsWithFire behaviour. Or… you could look at extending with interfaces to add functionality:
IBurnsWithFire, IHurtsAlot, IMakesYouCry
There are tons of different ways to accomplish it, but it’s hard without more precise context around how the items are constructed.
Yes,that’s exactly what i did,i used the exact same form of code you suggested.
And that was the whole problem: The editor won’t let me attach classes that extend BaseItem to a gameObject.
Wich means that if i go to the “ItemWichBurnsWithFire” prefab,and i select Add new Component> Scripts the “ItemWichBurnsWithFire” class won’t appear. I tried circumventing this by having a “Container” script that was a Monobehaviour and wich simply contained a public BaseItem variable,so that i could change THAT from the editor: Still no script showing up except the BaseItem script.
This hurts me physically.