- Home /
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);
}
}
}
}
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;
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.
it doesn't have a higher game object and it doesn't follow at all. It does have children. Could that be a problem?
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 ?
There aren't any animations on any of the highest game objects or their children. :(
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
Follow this Question
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