- Home /
Instansiate object into hand with a trigger, help...
Hi there,
I've been wrestling with the following Gun Pick up issue. I've tried lots of things and can't crack it.
I have a character with various body parts; head, arms, hands etc. I want to be able to pick up an object and attach it to the 'RightHand'. The character is set up as follows: Character, under which is the 'RightArm', under which is the 'RightHand'. The following script is attached to the 'Character':
using UnityEngine;
using System.Collections;
public class GunPickUp : MonoBehaviour
{
private GameObject _GameManager;
public GameObject RightHand;
public GameObject Gun1;
//Finds the 'GameManager'
void Start()
{
_GameManager = GameObject.Find("_GameManager");
}
//Locates the Tag 'Gun1', finds 'GameManager' and sets definition for 'PickUpGun'
void OnTriggerEnter(Collider other)
{
if (other.tag == "Gun1")
{
_GameManager.GetComponent<GameManager>().GunPickUp();
PickUpGun();
}
}
// InstansiateGun into RightHand
void PickUpGun()
{
{
// get a reference to the newly created object:
GameObject obj;
//Instansiates 'Gun1' into 'RightHand'
obj = (GameObject) Instantiate(Gun1, RightHand.transform.position, RightHand.transform.rotation);
// child the object to RightHand:
obj.transform.parent = RightHand.transform;
}
}
}
In the inspector it asks to give targets for the 'RightHand', which I've given as Gun1, and 'Gun1', which I've given as 'RightHand'. When I trigger the script, 'Gun1' instansiates on the scene, but not onto the 'RightHand' body part. In the Hierachy, the 'Gun1'clone appears on the scene. Any help on this would be greatly appreciated.
Rich
Your answer
Follow this Question
Related Questions
Prefab instantiating problem 1 Answer
Want a script that can instantiate an object when MOVING inside the trigger 1 Answer
Instantiate Prefab At Collided Object? 1 Answer
Spawn Point Problems 1 Answer
Parenting an instantiated Prefab? 1 Answer