- Home /
Question by
Kingda · Nov 16, 2012 at 05:25 PM ·
instantiatecloneenumbones
Second prefab(Clone) not finding Bones
Ok, Im having an issue with Instantiating a prefab. Currently, Im using Enum to differentiate upgradeable weapon meshes in a prefab. Calling them works fine, and parenting their roots works fine if there is only 1 (Clone) in the scene. The problem occurs when I spawn more than 1 clone. The first clone finds the root bone and parents it correctly, but Unity cant find the second root bone. What gives? Any suggestions would be appreciated!
#pragma strict
//WEAPON SET ASSIGN SCRIPT//
var character : GameObject;
var weaponSet : GameObject;
enum weapon {Weapon01,Weapon02,Weapon03,Weapon04,Weapon05};
var weaponLevel : weapon;
function Awake () {
print(character);
//find bone transforms
var b_BipRoot = character.transform.Find("b_BipRoot");
var b_Bip001 = b_BipRoot.transform.Find("b_Bip001");
var b_Pelvis = b_Bip001.transform.Find("b_Pelvis");
var b_LThigh = b_Pelvis.transform.Find("b_L Thigh");
var b_RThigh = b_Pelvis.transform.Find("b_R Thigh");
var b_LCalf = b_LThigh.transform.Find("b_L Calf");
var b_RCalf = b_RThigh.transform.Find("b_R Calf");
var b_LFoot = b_LCalf.transform.Find("b_L Foot");
var b_RFoot = b_RCalf.transform.Find("b_R Foot");
var b_LToe = b_LFoot.transform.Find("b_L Toe0");
var b_RToe = b_RFoot.transform.Find("b_R Toe0");
var b_Spine = b_Pelvis.transform.Find("b_Spine");
var b_Spine1= b_Spine.transform.Find("b_Spine1");
var b_LClavicle= b_Spine1.transform.Find("b_L Clavicle");
var b_RClavicle= b_Spine1.transform.Find("b_R Clavicle");
var b_Neck= b_Spine1.transform.Find("b_Neck");
var b_Head= b_Neck.transform.Find("b_Head");
var b_LUpperArm= b_LClavicle.transform.Find("b_L UpperArm");
var b_RUpperArm= b_RClavicle.transform.Find("b_R UpperArm");
var b_LForearm= b_LUpperArm.transform.Find("b_L Forearm");
var b_RForearm= b_RUpperArm.transform.Find("b_R Forearm");
var b_LHand= b_LForearm.transform.Find("b_L Hand");
var b_RHand= b_RForearm.transform.Find("b_R Hand");
var b_LWeapon= b_LHand.transform.Find("b_L Weapon");
var b_RWeapon= b_RHand.transform.Find("b_R Weapon");
//spawn weaponSet GameObject
var weaponToEquip = Instantiate(weaponSet, transform.position, transform.rotation);
//find weapon in weaponSet
var Weapon01 = weaponToEquip.gameObject.Find("Weapon01");
if (!Weapon01) print("Weapon01 not found");
var Weapon02 = weaponToEquip.gameObject.Find("Weapon02");
if (!Weapon02) print("Weapon02 not found");
var Weapon03 = weaponToEquip.gameObject.Find("Weapon03");
if (!Weapon03) print("Weapon03 not found");
var Weapon04 = weaponToEquip.gameObject.Find("Weapon04");
if (!Weapon04) print("Weapon04 not found");
var Weapon05 = weaponToEquip.gameObject.Find("Weapon05");
if (!Weapon05) print("Weapon05 not found");
//find weapon root bone
if (Weapon01){
var Weapon01_root= Weapon01.transform.Find("/b_Root");<=======Not finding this!
if (!Weapon01_root) print("Weapon01_root not found");
}
if (Weapon02){
var Weapon02_root= Weapon02.transform.Find("b_Root");
if (!Weapon02_root) print("Weapon02_root not found");
}
if (Weapon03){
var Weapon03_root= Weapon03.transform.Find("b_Root");
if (!Weapon03_root) print("Weapon03_root not found");
}
if (Weapon04){
var Weapon04_root= Weapon04.transform.Find("b_Root");
if (!Weapon04_root) print("Weapon04_root not found");
}
if (Weapon05){
var Weapon05_root= Weapon05.transform.Find("b_Root");
if (!Weapon05_root) print("Weapon05_root not found");
}
//parent, position, and orient weapon to character bones
if (weaponLevel==weapon.Weapon01){
if (Weapon01){
Weapon01_root.transform.position = b_RWeapon.transform.position;
Weapon01_root.transform.parent = b_RWeapon;
Weapon01_root.transform.localPosition = Vector3.zero;
if (weaponSet.tag == "Ranged"){
Weapon01_root.transform.localRotation = Quaternion.Euler(90,0,0);
}
else{
Weapon01_root.transform.localRotation = Quaternion.identity;
}
Weapon01_root.transform.localScale = Vector3.one;
Destroy(Weapon02);
Destroy(Weapon03);
Destroy(Weapon04);
Destroy(Weapon05);
}}
if (weaponLevel==weapon.Weapon02){
if (Weapon02){
Weapon02_root.transform.position = b_RWeapon.transform.position;
Weapon02_root.transform.parent = b_RWeapon;
Weapon02_root.transform.localPosition = Vector3.zero;
if (weaponSet.tag == "Ranged"){
Weapon01_root.transform.localRotation = Quaternion.Euler(90,0,0);
}
else{
Weapon01_root.transform.localRotation = Quaternion.identity;
}
Weapon02_root.transform.localScale = Vector3.one;
Destroy(Weapon01);
Destroy(Weapon03);
Destroy(Weapon04);
Destroy(Weapon05);
}}
if (weaponLevel==weapon.Weapon03){
if (Weapon03){
Weapon03_root.transform.position = b_RWeapon.transform.position;
Weapon03_root.transform.parent = b_RWeapon;
Weapon03_root.transform.localPosition = Vector3.zero;
if (weaponSet.tag == "Ranged"){
Weapon01_root.transform.localRotation = Quaternion.Euler(90,0,0);
}
else{
Weapon01_root.transform.localRotation = Quaternion.identity;
}
Weapon03_root.transform.localScale = Vector3.one;
Destroy(Weapon01);
Destroy(Weapon02);
Destroy(Weapon04);
Destroy(Weapon05);
}}
if (weaponLevel==weapon.Weapon04){
if (Weapon04){
Weapon04_root.transform.position = b_RWeapon.transform.position;
Weapon04_root.transform.parent = b_RWeapon;
Weapon04_root.transform.localPosition = Vector3.zero;
if (weaponSet.tag == "Ranged"){
Weapon01_root.transform.localRotation = Quaternion.Euler(90,0,0);
}
else{
Weapon01_root.transform.localRotation = Quaternion.identity;
}
Weapon04_root.transform.localScale = Vector3.one;
Destroy(Weapon01);
Destroy(Weapon02);
Destroy(Weapon03);
Destroy(Weapon05);
}}
if (weaponLevel==weapon.Weapon05){
if (Weapon05){
Weapon05_root.transform.position = b_RWeapon.transform.position;
Weapon05_root.transform.parent = b_RWeapon;
Weapon05_root.transform.localPosition = Vector3.zero;
if (weaponSet.tag == "Ranged"){
Weapon01_root.transform.localRotation = Quaternion.Euler(90,0,0);
}
else{
Weapon01_root.transform.localRotation = Quaternion.identity;
}
Weapon05_root.transform.localScale = Vector3.one;
Destroy(Weapon01);
Destroy(Weapon02);
Destroy(Weapon03);
Destroy(Weapon04);
}}
}
function Update ()
{
}
Comment