- Home /
Workarounds for Nested Prefabs
Hey fellas, after raging at Unity for not updating my nested prefabs, I did the research and realized that is simply does not support them. Welp!
The game I'm working on involves many different types of vehicles (effectively an RTS), but with shared parts (turrets, wheels, etc.) My instinct from using other programs was of course to make one prefab for each kind of shared object and then stick copies of it into each appropriate unit, but that of course doesn't work out when you need to make adjustments to them.
So the best solution, it seems, is to have a 'God' class with references to all prefabs, and instantiate them via code. When a change needs to be made to a unit, I would edit the appropriate .js.
But this requires potentially having .js for every individual type of instantiated object, means of determining where it should be mounted to the parent, etc.
I'll admit my jimmies are quite rustled on this issue. No native nested prefab support seems like a huge oversight. And I am very, very hesitant to shell out $35+ on a third-party plug-in, especially with a new release of Unity coming out soon (where there are some rumors of native nested prefab support... but again, shouldn't this have been something put in place years ago?)
Or, best case scenario: I'm just missing an obvious workaround, that hopefully someone can exemplify for me? :]
Thanks as always, guys!
I don't understand what you're asking. Are you saying that you can't have child-parent hierarchy in a prefab? If so, that is not true.
No, the problem is that if you have a prefab as the child of any other object and then you make a change to the prefab in the Project, the changes will not propagate to the childed instance.
That is, objects lose the link to their prefab once they are nested. And I find that absolutely ridiculous.
Thanks for the input, Fattie, but I think you've missed my entire point!
Say I have a couple of different tanks (different treads and armor and such) but I want them to have the same turret. $$anonymous$$akes sense that I should make a prefab out of the turret and stick one onto each tank.
But later, I decide I want to make that turret have two barrels ins$$anonymous$$d of one. So, I pick one of the prefab turrets, add another barrel, and hit "apply"... nothing happens to the identical turrets on the other tanks. >THAT< is what I find ridiculous.
$$anonymous$$y question is, since that doesn't work, what is the community-supported method of achieving such an effect?
And hey, at least I didn't cuss! ;)
And I'm not sure what 'button' you are referring to...
He's talking about a turret prefab IN a tank prefab
PrefabA with prefabB inside
Yeah, vethan4 has it.
I have two GameObject prefabs, "PlayerTank" and "EnemyTank". I want these tanks to have the same kind of turret, so I make a new GameObject called "TankTurret" and turn it into a prefab.
Now I put an instance of the TankTurret into both the PlayerTank and EnemyTank GameObjects.
I test the game, and I decide I want to make TankTurret a little bit different. I modify one of the TankTurrets, then hit 'apply'... but nothing happens to the TankTurret in the other tank! This seems silly to me!
Answer by thienhaflash · Mar 10, 2014 at 02:22 AM
I found this one, haven't try it out but might be that's what you need : http://framebunker.com/blog/poor-mans-nested-prefabs/
This right here is gold. Thank you! Awesome solution until Unity rolls out a native functionality.
Everybody use this!
Answer by vethan4 · Sep 21, 2012 at 11:16 AM
"So the best solution, it seems, is to have a 'God' class with references to all prefabs, and instantiate them via code. When a change needs to be made to a unit, I would edit the appropriate .js"
This. Have empty game object "anchors" for all the smaller prefabs and a script for each object. Each of your vehicles is gonna end up having at least 1 script attached to it anyway right? You just need to add the prefabs in at the Awake() part for each vehicle.
That's what I'd do at least in your case.
Thank you! I guess I will have to go along with this. No pain no gain I suppose, haha.
A follow-up question, if I may! In order for me to reference the prefab references in my 'God' class, I must set the variable types to 'static'. But with that, I cannot set the references in the inspector. The only way I've found to get around this is to have two variable declarations per prefab, plus an 'assignment' line:
var tankTurretPrefab : GameObject;
static var tankTurret : GameObject;
function Start(){ tankTurret = tankTurretPrefab; }
This results in three lines per variable, and these things are stacking up quick! Is there possibly a better way to deal with this?
One way of getting around that would be loading the prefabs from your Resources folder on a static initialize call (I'm a C# coder, so this'll look a bit different):
static GameObject tankTurret;
public static void Init() { tankTurret = (GameObject)Resources.Load("TurretPrefab",typeOf(GameObject)); }
That'll cut it down to 2 lines each?
That's an awesome shortcut, thanks! I will use that. Just gotta be careful not to rename my stuff. Cheers vethan4!
Answer by Bento-Studio · Mar 19, 2014 at 08:17 PM
Why not check out our plugin "nested prefab" from Bento here: http://u3d.as/content/bento-studio/nested-prefab/2Jz
Answer by PrefabEvolution · Mar 28, 2014 at 07:48 AM
Also another solution of Prefab Nesting http://forum.unity3d.com/threads/236719-Prefab-Evolution-Plugin
Again, because $$$. Stop your advertisements. Stop making bucks out of the situation we suffer from a basic function missing in unity.
Unity: Gives away nearly any mighty function for free. You: $$anonymous$$aking money out of our problems.
This sucks.
Your answer
![](https://koobas.hobune.stream/wayback/20220613081310im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiating a prefab in the editor is orphaning the children of the prefabs 0 Answers
Prefab variant changing all other variants. 1 Answer
Nested Prefabs in Asset Bundle 1 Answer
Are nested prefabs officially supported in unity 4.6+ at last? 1 Answer
"The prefab you want to instantiate is null" what am I doing wrong nested...? 1 Answer