instantiating prefab as child

hey everyone,

I simply cannot crack this one…

Im instantiating a prefab in my scene when dropping an object onto a trigger. works GREAT.
however, i wish for the prefab to be instantiated as a child to another gameobject and not just as a 0,0,0 point in my scene

currently my code is :

public class: Foodtray : Monobehaviour
{
public Transform Spawnpoint;
public GameObject Prefab;

void OnEnterTrigger ()
{
Instantiate(Prefab, Spawnpoint.position, Spawnpoint.rotation);
}
}

i looked into the unity docs on “Transform parent” and it might be simple, but i dont know how to implement it in my own code…

hope someone can help

all the bests

You can either use the parent parameter of Instantiate

Instantiate(Prefab, Spawnpoint.position, Spawnpoint.rotation, YourParent.transform)

or you can use transform.SetParent on your new GameObject

newGameObject.transform.SetParent(YourParent.transform)

Both of these will automatically change 0,0,0 to the origin of the new parent.

Well Transform parent is a transform component of object that you want to be parent object. For example, you can use this code: Instantiate(Prefab, Spawnpoint.position, Spawnpoint.rotation, Spawnpoint) to instatiate your prefab as a child object of Spawnpoint object.

How to Instantiate gameobject as child

** GameObject points = Instantiate(points_parent_prefab);
points.transform.SetParent(Gameobj));
RectTransform rect = points.GetComponent<RectTransform>();
rect.localPosition = new Vector3(0, 0, 0);
rect.anchoredPosition = new Vector3(0,0,0);
rect.localScale = new Vector3(1, 1, 1);
rect.sizeDelta = new Vector2(0, 0);
**