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 /
avatar image
0
Question by cidmodder · May 08, 2012 at 02:14 PM · c#instantiatechild

Making Instantiated Weapon a Child of the Player

I have a script that's supposed to let the player walk over a gun on the ground and press e and it instantiates a clone of the gun and makes that clone a child of the player so it follows it around. Right now it instantiates the clon facing the right way and everything but it doesn't become a child of the player. Any ideas? Thanks!

here's the script:

 using UnityEngine;
 using System.Collections;
 
 
 public class Player : MonoBehaviour {
     public bool equip;
     public Vector3 playerPosition;
     public Quaternion playerRotation;
     public GameObject Weapon;
     public GameObject equipedWeapon;
     
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     void OnTriggerStay(Collider other){
         Weapon = other.gameObject;
                 
         if (other.gameObject.tag == "weapon"){
             if(Input.GetKeyUp("e")){
                 playerPosition = transform.position;
                 playerRotation = transform.rotation;
                 equipedWeapon = Instantiate(Weapon,playerPosition,playerRotation) as GameObject;
                 equipedWeapon.transform.parent = transform.parent;
                 equip = true;
                 other.gameObject.SendMessage("Equip", playerPosition);
                 
             }
         }
         
     }
 
 
 }
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
1

Answer by Grim_Darknight · Dec 12, 2014 at 12:06 PM

Try changing line 30:

 equipedWeapon.transform.parent = transform.parent;

to:

 equipedWeapon.transform.parent = transform;
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 Berenger · May 08, 2012 at 02:39 PM

It should works. Unless there is some code elsewhere, I guess the weapon has an animation on it's highest (not higher, idiot !) gameobject which set the position somewhere else.

Comment
Add comment · Show 4 · 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 cidmodder · May 08, 2012 at 02:41 PM 0
Share

it doesn't have a higher game object and it doesn't follow at all. It does have children. Could that be a problem?

avatar image Berenger · May 08, 2012 at 02:56 PM 0
Share

Sorry about that, my bad english betrayed me. I meant highest, not higher. Which would be equipedWeapon here. Does that gameobject has an animation component with playAutomatically enabled ?

avatar image cidmodder · May 08, 2012 at 03:24 PM 0
Share

There aren't any animations on any of the highest game objects or their children. :(

avatar image cidmodder · May 11, 2012 at 01:48 AM 0
Share

I've played around with it and still haven't gotten the actual gun object to follow the player. The guns have children could that be the problem?

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

6 People are following this question.

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

Related Questions

Delete children of Instantiated object 2 Answers

Distribute terrain in zones 3 Answers

Destroying childs and Instantiate [C#] 0 Answers

GetChild(0) returns nonexistent object 1 Answer

Instantiating as a child error 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