- Home /
Linking a prefab to another prefab in deep hierarchy using Inspector
I'm trying to figure out how to have some UI Screen Panels link to one another. I'm considering each screen to be a prefab, and then I've created a script that attaches on a button that will connect a screen to another screen. In this way the user will be able to advance through screens.
The problem I'm finding is that when I drag these Panels into the scene, the prefab loses this reference. I've taken a screen shot below to indicate an example of the bolded reference (UIButton Flow Panel at the bottom right) that gets lost when the prefab enters the scene.
I suspect there is some issue with linking from deep in the object hierarchy.
I found this rather ancient forum post that suggests that such prefab-to-prefab linking only works at the top level of the hierarchy, not deep like I've done here. I've tested that and it seems to work. Is that really the only solution?
I want to be clear here. I don't want prefabs to link to objects in the scene. I know that that doesn't work. I want to have a prefabs link to other prefabs so that I can organize how they're connected at the prefab level, and instantiate them or drag them to the scene at some other time.
From my understanding I don't think I'm talking about Nesting prefabs. Nothing in the "PopupPanel" hierarchy for example is a prefab. I'm talking about having a child object of one prefab "PopupPanel" have a script that has a reference or link to another prefab "HackPanel."
O$$anonymous$$ yes you can. So in your script create a variable with the type of your object.
$$anonymous$$g. public BulletType bulletType;
where 'BulletType' is the class name.
If you have a prefab with the BulletType script on it, then you can drag that onto this variable field.
Note: $$anonymous$$ake sure that in your code you always instantiate an instance of this object before you modify any of its properties as doing that means you're modifying the prefab properties not the instance.
I've gotten that simple case to work, but not this deep hierarchy case. I'm going to edit the question to emphasize that it's not working in this deep hierarchy situation.
O$$anonymous$$ I don't think you can get that behavior that deep in the hierarchy.
Answer by DaveA · Sep 17, 2013 at 06:53 PM
" I'm talking about having a child object of one prefab "PopupPanel" have a script that has a reference or link to another prefab "HackPanel."
Your child object would have a script on it which has something like
var hackPanel : GameObject;
And you can set that via the inspector. It should be like any other variable on any other component: that it will have a default value (null in this case), and instances can override that in the Inspector or with code. You would drag from the Project pane the prefab you want onto this instance's hackPanel field.
Which looks like what you have done, right? And what's not working with that?
While instances of a prefab can have references to each other, you see$$anonymous$$gly can't have a prefab reference a prefab at that deep level of hierarchy. Looking at it now it simply seems to be an authoring issue. Unity won't display anything below one level for prefabs so it's impossible to author prefab to prefab connections.
Your answer
Follow this Question
Related Questions
Connecting scenes 3 Answers
How to find a prefab in another scene? 1 Answer
Preventing first instance of object from starting on screen 1 Answer
getting prefabs form project to another 3 Answers