- Home /
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.
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;
Answer by VisalDXP · Nov 24, 2014 at 12:46 PM
Try add this code below Instantiate line:
assaultRifleReal.transform.parent = yourControllerTransform;
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?
Try replace yourControllerTransform with hands
Transform. By doing that it should move and rotate along with the hands Transform
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
Follow this Question
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