- Home /
Set parent of an instantiated object where the object being instantiated is a transform
Hello. I am fairly new to Unity 3D and I am starting a project. The player crafts an item and as soon as it's crafted, the item falls to the ground. I want to attach a script to that object that OnTriggerEnter it destroys the object that has been created and re-instantiates the object in the hands of the player and when it is instantiated, it sets the hand of the player as the parent of the object. PLEASE TAKE NOTE:
This is my script being attached to the prefab (The object is a knife)
var InventorySystem: Inventory;
var SpawnPosition: Transform;
function Start()
{
InventorySystem = GameObject.Find("3rd Person Controller").GetComponent(Inventory);
SpawnPosition = GameObject.Find("AltItemPos").transform;
}
function OnTriggerEnter(Col:Collider)
{
if (Col.tag == "Player")
{
InventorySystem.itemEquipt();
}
}
And this is the function: itemEquipt() and the variables it uses
var knife: Transform;
function itemEquipt()
{
Destroy(PickUpScript.gameObject);
Instantiate(knife, PickUpScript.SpawnPosition.position, PickUpScript.SpawnPosition.rotation);
}
//How do I instantiate it and make it the child of SpawnPosition?
THANK YOU!
Answer by Jallen182 · Apr 23, 2014 at 12:19 PM
var knifePrefab: Object;
function itemEquipt()
{
Destroy(PickUpScript.gameObject);
GameObject newKnife = (GameObject)Instantiate(knifePrefab, PickUpScript.SpawnPosition.position, PickUpScript.SpawnPosition.rotation);
newKnife.transform.parent = PickUpScript.SpawnPosition;
}
It tells me to insert a semicolon in line 6...
GameObject new$$anonymous$$nife = (GameObject)Instantiate(knifePrefab, PickUpScript.SpawnPosition.position, PickUpScript.SpawnPosition.rotation);
(UCE0001: ';' expected. Insert a semicolon at the end.)
Your answer
![](https://koobas.hobune.stream/wayback/20220613143359im_/https://answers.unity.com/themes/thub/images/avi.jpg)