how to change hierarchy on prefabs - unity3d

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

Related

In unity why cannot Instatiate a gameobject

I am currently coding in Unity and I am trying to Instantiate a gameObject. The mechanism I'm developing will delete the Image I'm on and load the image underneath, using two buttons ; One to create and one to delete thus giving the effect of moving through a graphical stack.
I have thought about moving the image forward along the z axis so it is out of view, but I have read that the object MUST be instatiated and not Transformed
I have a Rigidbody2d and (2x) 3D Box Collider over the image that has the code. One on the left and one on the right
The delete button works a treat and just to check I do not have a problem with the box collider I have tried the delete code on the other button which also works fine, so I have come to the conclusion that the code is flawed in some respect
My problem is that I cannot seem to recreate the object once it has been deleted, it is my understanding that the object does not have to exist in world space for it to be recreated. If I use the Quaternion Identity I get a few errors about the gameObject being rendered off screen
***
Instantiate(gameObject,new Vector3 (8,-17,11),
// Removed until I can actually get the image to Instantiate
Quaternion.identity);
***
I have also tried
***
Instantiate(gameObject,new Vector (gameObject.transform.position.x -0.08, gameObject.transform.position.y, -0.17, gameObject.transform.position.z, 11), Quaternion.identity);
}
}
***
Can someone please tell me what I am doing wrong?
I am fairly new to Unity too, but I believe what you need is a Button that Swaps the Sprite image.
Select your Button in the Hierarchy.
In the Inspector:
In the Image component, drag in the first Sprite
In the Button Component:
select the check box Interactable
select Sprite Swap in the Transition Drop Down
drag your second Sprite to "Selected Sprite"
I am fairly new to Unity too, but I believe what you need is a Button that Swaps the Sprite image.
Select your Button in the Hierarchy.
In the Inspector:
In the Image component, drag in the first Sprite
In the Button Component:
select the check box Interactable
select Sprite Swap in the Transition Drop Down
drag your second Sprite to "Selected Sprite"

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.

Unity deforms every prefab that I create

I have problem with Unity prefabs. Every time I create a nice looking UI in main scene and I want to store it as prefab for later usage if I open this prefab it's deformed(random cliping, moved UI elements in different directions, ... such a mess). If I try to fix this prefab in prefab editor to make it nice looking like before and then if I insert this back to scene it's totally unusable. It creates bigger mess than before in prefab editor... I don't know what to do. Editor throws no errors btw.
In first image is nice looking UI(example already created). In 2nd image is prefab editor. Text is totally out of panel and 3rd is from back in scene (means removed old one UI and place stored prefab from folder) the panel is gone.
Imgur - images are orded by scene to prefab editor and back to scene
On those images you can see a 3 elements... but if I make more complex UI, it's clipping through each other in prefab editor and also back in scene.
Unity version: 2019.1.7f1

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.

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

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.