For some reason, I can’t seem to save an instantiated object to a GameObject. My code looks like:
public GameObject weaponPrefab;
BoneHelper boneHelper;
Transform weapon;
// Use this for initialization
void Start () {
boneHelper = GetComponent<BoneHelper>();
boneHelper.Init();
GameObject weaponObj = (GameObject)Instantiate(weaponPrefab);
print("Weapon Obj: " + weaponObj);
weapon = weaponObj.transform;
Transform rightHandBone = boneHelper.GetBone("hand.r");
if(rightHandBone != null)
{
weapon.parent = rightHandBone;
}else
{
print("Could not fine bone 'hand.r'");
}
}
The error “Cannot cast from source type to destination type” happens at the Instantiate line. If I use ‘as’ instead of the more common cast syntax, the weaponObj print out looks like "Weapon Obj: " with no object being shown and the next line will give a null pointer error.
I’ve never had this problem before and I’m pretty sure it should work. Anyone have any idea why it’s complaining?