- Home /
prefab question
I have a model of a gun. I've brought it into unity and added some components and then made it a prefab.
There are scenarios in which I only want to instantiate one of it's children.
Is it possible to instantiate just one child of a prefab or will i need to make a whole bunch of separate prefabs for each piece?
Screen shot showing the prefab with it's children expanded: http://www.gordonjennings.net/temp/C14prefab.PNG
I guess if the child is also a prefab you should be able to do that..
I mean. For example the prefab shown in the screen shot is called "C14a". I can instantiate that and BA$$anonymous$$! I have the whole gun. But it won't let me instantiate the children of the prefab on their own. For example if I want to instantiate the Scope by itself, I have to make a new prefab of just the scope. Is there some special syntax I could use to instantiate children of prefabs? So that I don't need to go through this whole thing and make a new prefab for each piece.
oh that.. yeah I don't think you can.. or I have no idea how..
I would in this case have some prefab A, B, C .. then have another master prefab (for example ABC using the 3 other prefabs..)
hoping that makes sense ;-)
well basically this is for an application that allows people to assemble/disassemble weapons with rules enforced so they have to follow a sequence. So for example you can't remove the cheek plate until you remove the cheek plate screws.
One of the features I've implemented is that there is a second camera that is placed in the upper left corner on top of the main camera view. When the user clicks on any piece that piece becomes highlighted (scripting manipulates material colors to give a glow effect). The highlighted piece gets cloned and placed in front of the second camera so it appears in the upper left of the screen. Info about the piece is displayed and the user can rotate the piece in the smaller window. As shown in this screen shot: http://gordonjennings.net/temp/highlightWindowExample.PNG
The problem is that some pieces have geometry such that the program will freeze briefly when it's being instantiated. I figured prefabs would speed up the instantiation in contrast to clones.
The reason I wanted to do what my original post suggested is so I don't have to make prefabs for all the pieces and just reference the main prefab.
Oh well, unless anyone comes up with an idea, I'll have to do just that :(
Answer by DaveA · Oct 27, 2011 at 03:33 PM
I suppose you could instantiate the whole thing, then turn off or delete the parts you don't need. But you might also be able to create a new object using AddComponent(Mesh) and so forth to build an object from mesh and materials. Not really clean either way. You could also write an Editor script which could dissect your models and build all those sub-prefabs for you.
Vote for nested prefabs at feedback.unity3d.com
That sounds good, 1st unparent what you want and then delete the unwanted.
But really I would just make more prefabs.
well good news guys. Before exploring what I was trying to do, I had no idea there were editor scripts. So i made one to quickly make prefabs out of all the children of the main prefab. The parts load instantaneously when clicked on now : ]