I'm writing a script (in C#) which, on mouse down, will spawn a gun and parent it to a game object. But for some reason the gun is instantiated just fine, but it doesn't parent. I have an empty game object called "Hands" and I'm trying to have the gun become a child and be moved to the position of "Hands".
To test what was going on I made two scripts, one which would parent the object upon pressing P, while the other script would just instantiate the item on mouse down. It worked, I first spawned the gun with mouse down, and then pressed P and it was parented. So my only guess is that you can't instantiate and parent in the same script all at the same time.
I have absolutely no clue what's going on, so I really appreciate any help I can get.
This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GiveWeapon : MonoBehaviour {
public GameObject Weapon;
public GameObject Hands;
void OnMouseDown ()
{
Instantiate(Weapon);
Hands = GameObject.Find ("Hands");
Weapon.transform.parent = Hands.transform;
Weapon.transform.position = Hands.transform.position;
}
}