-1

I wish to have an interface with multiple buttons (Astronaut, Drone, Canister etc.) which then appear over the Ground Plane when the user presses a button.

I'm having a hard time making my prefabs as a child of the Ground Plane Stage dynamically. I'm also not entirely certain on the working of ContentPositioningBehavior script which seems to do it.

Can anyone please help? Thanks.

  • Did you look at the API / do some research? [`var newObject = Instantiate(prefab, groundPlane.transform);`](https://docs.unity3d.com/ScriptReference/Object.Instantiate.html) or [`newObject.transform.SetParent(groundPlane.transform)`](https://docs.unity3d.com/ScriptReference/Transform.SetParent.html) or [`newObject.transform.parent = groundPlane.transform;`](https://docs.unity3d.com/ScriptReference/Transform-parent.html) ... – derHugo Apr 02 '20 at 05:51
  • @derHugo, yes I did but I had a hard time understanding that it was the transform component was the one that we needed. My apologies, I'm a complete beginner at this. But thanks for your help. – Redford Dirk Apr 02 '20 at 15:31

1 Answers1

1

There is an overload for Instantiate(), which is

Instantiate(Object object, Transform parent)

In order for instantiated gameObjects to have ground plane as a parent, you can just pass it as parent parameter. Like:

[SerializeField]
private Transform groundPlane; // Drag ground plane to this in inspector.

private void OnClickAstronautButton()
{
    Instanitiate(astroPrefab, groundPlane);
}

...

Also, there is Transform.parent field. You can assign to this field, like

astronaut.transform.parent = groundPlane;
MyBug18
  • 2,135
  • 2
  • 11
  • 25