Unity instantiated object not sticking to its parent?

Hello everyone, So im creating a basic Unity inventory system that uses keys 1-3 to switch items.

I currently have this code.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class playerInventory : MonoBehaviour {
	public int currentWeapon;
    public GameObject test;
	public int selected = 0;


	List<GameObject> inventory = new List<GameObject>();

	// Use this for initialization
	void Start () {

		inventory.Add(test);
		Debug.Log (inventory.Count);


	
	}
	
	// Update is called once per frame
	void Update () {
		loopThroughBackpack ();
	}

	void loopThroughBackpack(){
		if (Input.GetKeyDown (KeyCode.Alpha1)) {
			selected = 0;

			GameObject.Instantiate(inventory[selected], transform.parent.position, transform.parent.rotation);



		}
		else if(Input.GetKeyDown(KeyCode.Alpha2)){
			selected = 1;


		}

		

			}		
		}

Now on the If statement where i instantiate the selected object called testCanteen i set its position to the main camera however when it spawns it just floats in the air, what i would like is to have it stuck to the 1st person controller (Non instantiated).

Any help is appreciated thanks a tonne!

You need to define public Transform controller and pick it in inspector, and then attach instantiated object to it:

GameObject go = (GameObject)GameObject.Instantiate(inventory[selected], transform.parent.position, transform.parent.rotation);
go.transform.parent=controller;

Hi, as I understand it, just Instantiating the object using the coordinates doesn’t put the object in the players hierarchy. You need to set its parent to be the player.

GameObject item = GameObject.Instantiate(inventory[selected], transform.parent.position, transform.parent.rotation) as GameObject;
item.transform.parent = gameObject.transform;

should do the trick (at least, something in the line of that). There might still be some problem, though but I am not sure.