I'm having hierarchy related trouble. I have two characters in my scene with identical hierarchies parented under different Game Objects (Player_1 and Player_2, originally). I want to parent an object (a rucksack) to a specific joint on both characters and thought I could simply apply this script to the Player_1 & Player_2 nodes:
var rucksack:GameObject;
var targetPlayer:GameObject;
var joint:GameObject;
joint = targetPlayer.Find("joint_1");
function Update()
{
rucksack.transform.parent = joint.transform;
}
Although, it doesn't seem to look through the "targetPlayer" hierarchy, instead attaching both rucksacks to one of the characters joints. Where am I going wrong?
The problem is that you're using a static function like an instance function, hiding what it'll actually do. GameObject.Find finds a named object in the scene, with javascript allowing it to be called on an instance for some reason.
Either way - you could try using targetPlayer.transform.Find("joint_1"), though I can't remember if Transform.find actually searches the hierarchy or not - if it doesn't, you could use the full path, e.g. targetPlayer.transform.Find("bip_01/hip/spine/joint_1");
find will return the first object with this name in the scene
if there s a lot of them it will probably return crap so
if you need to acces any part of hierarchy , rename it with it s instance id in start gunction
in targetPlayer
Start()
{
var j = GameObject.find (“join_1”) // the right one becaus all that have been spawned are already renamed by next line
join_1.name+=GetInstanceID();
}
from other object
targetPlayer.find (“join_1”+targetPlayer.GetInstanceID()) allway return the right one