How can you instantiate a prefab as a child of another object?

Hello,
I’m familiar with instantiating a prefab generally.
I would usually specify a Vector3 during the instantiation process, which would place it in those exact coordinates on the screen.

However it’s probably far better practice to add the item to the hierarchy it belongs to and specify it’s coordinates relative to it’s parent.

Can someone please explain how I can do this?

Thank you.

You could set the parent and local position after its been instantiated. Depending on what type you need to instantiate you’ll need to target the objects transform, or you could instantiate as a transform

public Transform parent;
public Transform prefab;

Transform clone = Instantiate(prefab) as Transform;
clone.SetParent(parent);
clone.localPosition = new Vector3(0, 2, 0);
1 Like

Instantiate, set the parent property of its transform, set position.

brilliant, thanks guys.
It looks easier than I was expecting.