0
\$\begingroup\$

I have a game object that I want to instantiate. There is no problem at all with the Instantiate() method. But the problem is, whenever I instantiate the object I want it to be a child of another object (like in the hierarchy). For example, a keyboard object is a child of a notebook object in the hierarchy.

Whenever I instantiate the keyboard, I want it to be a child of the notebook object.

How to do this?

\$\endgroup\$
1

1 Answer 1

1
\$\begingroup\$

As mentioned by user Zibelas, you can use the Instantiate(Object original, Transform parent) function to instantiate an object. This will immediately set parent as its parent after its creation, thus it will become a child of the parent Transform. Usage:

GameObject newObject = Instantiate(originalPrefab, newParent);

This is equivalent to doing this:

GameObject newObject = Instantiate(originalPrefab);
newObject.transform.SetParent(newParent);
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .