- Home /
Prefab inheritance without scripts. Is it possible?
This is more of a hypothetical quesiton than a syntactic one. Still, if I can do this it would be convenient to say the least, and if it's in the manual, I probably missed it somewhere.
The best example I can think of is like the voxels in Minecraft. Say I have a whole bunch of prefabs with common traits - they're uniform cubes, you can walk on 'em, mine 'em, and most of them can spawn drops. But at the same time they're all different in what they can survive, what they spit out, etc.
The blocks I'm gonna be working with have most of the same basic concepts - so is there any way I can just assign them all the same solid, minable parent (pretty literally a super-prefab with sub-prefabs under it), or do I just have to copypaste some sort of class/inheritance script in every 'block' prefab type I make?
Answer by Slobdell · Aug 18, 2013 at 12:00 AM
I think what you're talking about IS in fact scripts, just have a script with the basic characteristics and assign to all, then have a seperate script for each different type of block that gives it it's unique traits. Or have the same script check which type of block it's assigned to and behave accordingly.
The problem with this logic is that if my script has parameters on it such as hit points and speed.
I want to be able to change those on the inherited prefab too. If I don't change it, I want the defaults from the original prefab to be used and modified if the original is edited by a designer.
have you tried it? as far as i can tell you are describing exactly how unity handles inheritance. if something is working not as expected, we can probably help better.
edit: or are you saying you want subOBJECTS to be a subclass of their parent's type? that setup is going to introduce a lot of headaches, but you can automate adding in the right components with a custom editor for the base class that provides a list of options for its chilfren to be
I know all about script inheritance. I'm talking about prefab inheritance.
One prefab would have a mesh renderer on it and some scripts. I make a child of that prefab, it also has the mesh renderer on it. If I remove the mesh renderer from the parent, the child also has it removed. I can change the mesh on the child's mesh renderer without affecting the parent.
At the very heart here I'm talking about inheritance of a gameobject's component stack.
After further investigation I believe this can be done with reflection, but its a lot of work.