• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by topkekker · Oct 23, 2014 at 01:18 PM · c#inventoryinstantiate-objects

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MarksTeq · Oct 23, 2014 at 02:14 PM 0
Share

@topkekker Check out your previous post, I edited the answer a bit :) Add an Item to a List

avatar image topkekker · Oct 23, 2014 at 02:28 PM 0
Share

@$$anonymous$$arks Teq when i try your way of parenting i get this error:

Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption. UnityEngine.Transform:set_parent(Transform) playerInventory:loopThroughBackpack() (at Assets/playerInventory.cs:32) playerInventory:Update() (at Assets/playerInventory.cs:24)

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer Wiki

Answer by DerickTP · Oct 23, 2014 at 01:35 PM

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.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image topkekker · Oct 23, 2014 at 02:11 PM 0
Share

Assets/playerInventory.cs(31,36): error CS0266: Cannot implicitly convert type UnityEngine.Object' to UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)

Thats my error.

avatar image DerickTP · Oct 23, 2014 at 02:15 PM 0
Share

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

Sorry I forgot the as GameObject conversion...

avatar image topkekker · Oct 23, 2014 at 02:16 PM 0
Share

Now i get this:

Assets/playerInventory.cs(32,40): error CS0029: Cannot implicitly convert type UnityEngine.GameObject' to UnityEngine.Transform'

avatar image DerickTP · Oct 23, 2014 at 02:20 PM 0
Share

Again my bad I write too fast. I also don't check my answers very well :-) I am at work and can't verify everything. I edited my original answer... hope this helps

avatar image topkekker · Oct 23, 2014 at 02:30 PM 0
Share

This works perfectly, However how would i go about say adding it slightly off to the left like its in the players hand?

Show more comments
avatar image
0

Answer by Itaros · Oct 23, 2014 at 01:22 PM

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;
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Unity add a item to a list? 2 Answers

unity3d simple inventory 1 Answer

How can I delete an item from this inventory system? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges