- Home /
The question is answered, right answer was accepted
FindChild not working? (Pick up Object)
I cannot get this child of an object to become the transform weaponHandle in this code, it just makes it empty.
What this code is supposed to do is pick up a weapon and drop the one already in the hand. It works but there is a problem with the weaponHandle, which is not being found and then becomes null.
It is using a raycast to hit the gun and then do the rest.
code:
if(Physics.Raycast(aimray, out aimhit)){
if(aimhit.transform.tag == "weapon"){
if(Input.GetKeyDown(KeyCode.Q)){
aimhit.transform.parent = handR;
aimhit.transform.position = handR.position;
weaponExtraHandle = aimhit.transform.FindChild("extrahandle");
Destroy(aimhit.transform.rigidbody);
weapon01.GetComponent<Rifle>().enabled = false;
weapon01.transform.parent = null;
weapon01.AddComponent<Rigidbody>();
weapon01 = aimhit.transform.gameObject;
}
Please help.
My game WIP: http://forum.unity3d.com/threads/200629-Omega-Void-WIP
Please post the full code, just edit the question with it all added and I'll be happy to throw it in Unity and troubleshoot for you :)
Holy crap its a massive code with other codes attached to it. Is there nothing you can tell me from this?
Its the weaponHandle FindCHild which is not finding the child in the object the transform hits.
It recognises the hit and parents the gun, drops the held one etc. Only the weaponHandle is not being found.
Well with the little info you provided, all I can tell you to have a look at Transform.Find("Name of Object you wish to find"). You'll notice if you refer to the Script Ref, like $$anonymous$$eat5k says, FindChild is not in the docs.
FindChild works though, but I have no idea what the difference between Find and FindChild could be.
Either way I fixed it. I put the answer below.
Answer by DeadKenny · Oct 08, 2013 at 01:26 AM
I had to directly reference the location of the child in the
weaponExtraHandle = aimhit.transform.Find("connectBarrelBone/barrel/handle");
Accept your own answer and +1 to comments/answers that helped :)
for showing a commonly unknown way to use transform.Find
$$anonymous$$y first answer, makes me feel less dumb lol.
for posting resolution and upvoting helpful comments / answers.
Answer by meat5000 · Oct 08, 2013 at 12:58 AM
I don't see FindChild in the docs.
It's transform.Find("ChildObjectName") or transform.GetChild(intIndex)
Find and FindChild seem to do the exact same thing. It makes no difference.
Does aimhit have a child object?
Does object extrahandle actually have a space or caps in the name?
Oh my god ok solved it.
I had to directly reference the location of the child in the
transform.Find("connectBarrelBone/barrel/handle");
I had tried it before but like a complete dipS!@t I kept missing /barrel/ part.
Anyway. Thanks for feedback.
Follow this Question
Related Questions
transform.Find(string)? 2 Answers
Why GameObject.Find() work and parent.transform.Find() doesn't work? 1 Answer
How can I find a specific child transform of the object in the trigger? 1 Answer
Finding Children question 3 Answers
about transform find 2 Answers