How can you make a prefab that can expand to show its constituent objects? - unity3d

I am very new to Unity3d. I want to make an prefab that expands with a little right facing button to show other objects. I have tried giving the prefab children and then dragging it into the project window, but it didn't show the little right-facing arrow. Can anybody help?

To add components to a prefab, add it to the scene as you said you have, then drag your next object on top of the prefab in the Hierarchy window. Sometimes this places it relative to the prefab but does not actually child it, so simply drag the object now in your Hierarchy onto your prefab once more and it should fall in place as a child, you will know this worked if you now have an expand node next to your prefab.

First, make your root game object that you will convert into a prefab.
In order to add objects which are children, right click on the root object and then make the children. You can also drag other gameobjects onto the root game object to make them children of the root game object.
Finally, drag your final prefab into window where all of your files are, and the resulting prefab will have children.
If you want to change the prefab after it is made, you have to change one object that results from your prefab and after changing it, click Apply in the top of the Inspector window to apply that change to all other objects resulting from the same prefab.

Related

how to change hierarchy on prefabs

I am new to unity and working on a image viewer in unity2D.
I am not sure if I choose components true but for now working fine.
most of the project is script based
I created a prefab which has a raw image
and in script I Instantiate prefabs in a scrollviews content
images are displayed no problem in that
I added some events where I Instantiate my prefab pointerDown, beginDrag, endDrag
dragging workin fine too
but when I drag any added prefab to older added prefab it stays on top but if I drag it to a newer added prefabs it stays under them.
I think its because the hierarchy order.
how can I change the order or how can I manage to make prefab stay on top when dragging
ps: on the image thumb_0 is added first and after drag it stays on behind thumb_2
if you can access the Transform of your prefabs you can use the method SetAsLastSibling(); it will move your gameobject to the last spot under your parent object, therefore causing it to be drawn on top
https://docs.unity3d.com/ScriptReference/Transform.SetAsLastSibling.html
EDIT :unless you mean dragging it manually, so then drag it to the bottom , last item always appears on top

Cant drag global gameObject into new unity nested prefab system

Im just starting to use the new Unity nested prefab workflow (or what ever you call it) in Unity 2018.3
So how it works is when you want to edit a prefab, you select it then click on the arrow. When you do this it erases everything from the hierarchy window and then you only have that prefab visible until your done editing it, you click save and then its back to the original hierarchy that was there before you started editing.
This is all fine, but the problem is what if i want to drag a game object from my scene into the prefab im editing is now impossible because as soon as you start editing the prefab, it removes your scene and only shows the contents of the prefab. So you cant drag anything from your scene into the prefab any more because its gone for the duration of the editing.
I dont know if im making myself clear or not.... But basically im saying while editing the prefab, only the contents of the prefab is visible, your scene is not visible, so you cant drag any object from your scene into the prefab anymore.
Anybody knows how to get around this? Right now i want to drag a gameObject from my scene into a prefab im editing to make a reference to a global object, but while editing the prefab my scene is not visible, so i cant do it.
Thank
Just create a copy of your prefab in Hierarchy and make you required changes to that copy once done, drag and drop that copy on you prefab and it will be updated.
When dropping the copy on your prefab you can either create a new prefab or update the original one.
Editing a prefab can be done in multiple ways. To achieve what you want, you should drag an instance of the prefab into your scene where your "other" gameobject is. Ensure that the prefab instance is blue, so that you know that it is still connected to the prefab.
Now, drag your "other" gameobject onto the prefab instance, so that it becomes a child of it. Now you should be able to apply prefab changes through the inspector.
Do note, if you cannot change the position in the hierarchy of the "other" gameobject onto a prefab from within the scene view. If you want to change the position, you should open the prefab scene after applying changes.

Cannot attach GameObject to a button's On click enviroment within a prefab. Unity

So I have a prefab which containcs another prefab with a group of buttons. So in one of the buttons I want to attach on the onClick enviroment a gameObject which is in my scene to get its scripts and finally its methods. I can attach just the script but I cannot find the methods of it(They are public). I can't look into the prefab and at the same time at the game object so that I drag and attach into the onClick button. When I click one the other disappears. Any suggestions?
Here is the picture of the prefab that containts the prefab of the group of buttons. Which has the UI Script (that I dragged and dropped) but doesnt have its functions(Public).
And here is the Canvas(Game object) which has the Script on it (UIManager) that I want the button to get its methods from. But when I click to canvas the prefab dissapears so I cant drag and drop it.
Thank you...
Right click on the Inspector tab label and select Add Tab -> Inspector. Drag the second inspector below the first. Click on your first object, and then click on the little padlock icon in the upper-right of the first Inspector. Now click on the second object. You can now drag-and-drop things between Inspectors.
You can attach objects to the instance in the Hierarchy, regardless of the Hierarchy instance being a prefab. You may want to approach this with some thought as to what constitutes a prefab (read: template) and what is just an object living in the Hierarchy.
You can't attach Scene objects to the prefabs in your Assets.

Why isn't Unity allowing me to apply certain changes to a prefab?

I have a prefab called square in the asset tray, I drag it into the scene to create an instance of the square prefab (in the hierarchy it is highlighted blue and has the select, revert and apply buttons in the properties). If I move/resize the instance and then press apply, the changes are applied to the prefab.
However, the square prefab has a script with a public game object. I drag the game object from the hierarchy into the slot in the instance's properties tab and is shows that the script is now referencing the actual gameobject. However, when I press apply this change isn't applied to the prefab (the prefab's script still isn't referencing any actual gameobject). All other changes to the prefab are applied.
I have tried dragging the instance into the asset tray to create a new prefab with the changes, but, as soon as I do, the script no longer references the game object.
I shouldn't have to create an entirely new prefab every time I add a public variable to a script so why can't I apply this change?
Ok you can't really drag an instance to a prefab. As a rule of thumb keep instances with instances and prefabs with prefabs.
I would instead find the instance from the prefab when this one is instantiated, with a method like findobjectoftype, or gameobject.find. Let me know if you want me to expand the answer
Solved: Prefabs can only reference other prefabs since there might not be an instance. Made the actual gameobject into an instance of a prefab and applied the prefab to the script
Prefabs are Assets. You can reference only other Assets to an Asset, never instances of Assets.

Can't apply prefab changes

i have a problem when try to apply changes.
I have a GameObject in the hierarchy, i put other GO in a custom script and push apply button. All has saved, but 2 game object can't apply canges.
Thanks in advance.
If you add new objects to the transform hierarchy, those new objects can't reference the prefab because they don't know they're part of the prefab. You can tell this because in the screen hierarchy list the prefab items are blue and the rest are not. You have to save the changes from an object that is part of the prefab first or redefine the prefab by dragging the parent object onto the prefab on the project list. Because a direct save was possible, it will overwrite.
The same is still essentially true for removing objects from the prefab hierarchy, except Unity knows that this is a breaking change (because it wouldn't be mappable to the prefab anymore) and informs you that doing so well remove ALL of the objects from the prefab upward reference and you can only save the changes by redefining the prefab by dragging it from scene to project (and possibly receive another warning about the objects being different: are you sure you want to overwrite?).
Likely those Text and Transform objects are found outside of the prefab. Prefabs can't reference external objects, since there's no guarantee they'll be available for every instantiation of the prefab.