Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by CheeseScalibur · Nov 24, 2014 at 12:22 PM · instantiateparentchild

How do I allocate a parent to a gameobject that just got instantiated?

I've been working on a pickup system for a gun in my game and I want the gun to get instantiated if you press 'e' on it and become a child of my character controller. Here's my script for the pickup and stuff

 using UnityEngine;
 using System.Collections;
 
 public class GunPickup : MonoBehaviour 
 {
     public bool gunPickup;
     public Font textFont;
     public bool guiShow = false;
     public GUIStyle guiFont;
     public GameObject gun;
     public GameObject realGun;
     public Transform hands;
     public Transform center;
 
 
     void Update ()
     {
         transform.Rotate(Time.deltaTime, 0, 0);
         transform.Rotate(0, Time.deltaTime, 0, Space.World);
         if(guiShow == true)
         {
             gunPickup = true;
         }
         else
         {
             gunPickup = false;
         }
         if(Input.GetKey(KeyCode.E))
         {
             Destroy (gun);
             Rigidbody assaultRifleReal;
             assaultRifleReal = Instantiate(realGun, hands.position, hands.rotation) as Rigidbody;
             
         }
     }
     void OnTriggerEnter(Collider coll)
     {
         if(coll.gameObject.tag == "Player")
         {
             guiShow = true;
         }
     }
 
     void OnTriggerExit (Collider coll)
     {
         if(coll.gameObject.tag == "Player")
         {
             guiShow = false;
         }
     }
 
     void OnGUI (){
         GUIStyle style = GUI.skin.GetStyle ("label");
 
         if (guiShow == true)
         {
             GUI.Label(new Rect(207f, 260f, 500f, 50f), "Press 'e' to pick up Assault Rifle", guiFont);
         }
 
     }
 }
 

It Makes the 'gun' rotate on the floor as a pickup and when you walk onto the trigger, it displays a message at the centre of the screen telling you to press 'e', when you do, it instantiates the real gun in and it just floats there. I need help to get it to follow with the player's movements. Sorry for adding the entire script, I'm new to the question asking thing so I don't know what I'm supposed to put in.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Curkovic · Nov 24, 2014 at 02:14 PM

You should attach gun as child to hands:

 assaultRifleReal = Instantiate(realGun, hands.position, hands.rotation) as Rigidbody;
 assaultRifleReal.transform.parent = hands; 

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
avatar image
0

Answer by VisalDXP · Nov 24, 2014 at 12:46 PM

Try add this code below Instantiate line:

 assaultRifleReal.transform.parent = yourControllerTransform;
Comment
Add comment · Show 3 · 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 CheeseScalibur · Nov 25, 2014 at 10:05 AM 0
Share

It doesn't work for me, I have assigned yourConrollerTransform a transform but it only instantiates in the direction that the character is facing, is there any other way to do it? Or am I doing something wrong?

avatar image VisalDXP · Nov 25, 2014 at 12:00 PM 0
Share

Try replace yourControllerTransform with hands Transform. By doing that it should move and rotate along with the hands Transform

avatar image VisalDXP · Nov 25, 2014 at 12:15 PM 0
Share

If it still doesn't work then try to instantiate your gun prefab in the Editor and put it in your character's hand (or wherever you think that is the right place) then rotate and position it as your desire. After its position and rotation are correct, disable it (don't delete it) and go back to your script and declare a variable "fake_gun" which is hold a reference to that gun Transform. Then add the instantiate line:

 assaultRifleReal = Instantiate(realGun, fake_gun.position, fake_gun.rotation) as Rigidbody;
 assaultRifleReal.transform.parent = fake_gun.parent;


Hope this might help :)

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Make a simple tree 1 Answer

Renaming Child Objects during Instantiate? 1 Answer

Instantiate Terrain Object as child of Empty Game Object 1 Answer

Getting instance of an sub object rather than the original's subobject 0 Answers

[SOLVED] How to make to clone an object and make it parent of the transform? 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