I'm trying add a weapon to Eithan - the third person character. So I want to take his hand, which is part of the prefab Eithan (the weapon location) and use it in the my Player.cs script to Instantiate the weapon. I created a parameter in the script for the hand: [SerializeField] GameObject weaponSocket; and I tried to Instantiate the weapon like this like this: Instantiate(weaponPrefab, weaponSocket.Transform);, but I just cant drag the hand to the parameter.

Anyone knows how to do this correctly?

Thank you!

Images: Player script: https://imgur.com/RcvKeXN Trying to drag the left hand to the parameter (indicated by red arrows): https://imgur.com/RB63c9d

  • 1
    \$\begingroup\$ Try changing it to [SerializeField] private Transform weaponSocket; Let me know if it works for you? \$\endgroup\$ Jul 2 '20 at 6:56
  • \$\begingroup\$ didnt work, same thing happens \$\endgroup\$
    – roni
    Jul 2 '20 at 7:33
  • 1
    \$\begingroup\$ In your screenshot, it looks like you've selected your player prefab in the Assets folder, then tried to drag the hand from the instance in your open scene into its inspector. This will not work because a prefab cannot reference a scene object. You should select the Player instance in your scene, then try it on the instance. Then you can apply that change back to the prefab in your assets folder by applying the override. \$\endgroup\$
    – DMGregory
    Jul 2 '20 at 11:01

What you can do is to use the method FindGameObjectsWithTag. Add a special tag to your hand and use this:

GameObject hand = FindGameObjectsWithTag("HandTag")[0];

Here is the documentation: https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html

  • \$\begingroup\$ Keep in mind that Find does a string comparison with every child of the game object, which can be quite slow for more complex ones. So it should be voided if possible. And if you have to use it (usually you don't) you should cache the result. \$\endgroup\$
    – Philipp
    Jul 2 '20 at 7:07
  • \$\begingroup\$ Yes clearly, but his script will only use it one time I think. So it won't be time consuming. But it surely exists a better idea \$\endgroup\$
    – Adrien G.
    Jul 2 '20 at 7:09
  • \$\begingroup\$ Hey, just tried it, it works for EthanBody or hips, but for some reason it returns null for the hand \$\endgroup\$
    – roni
    Jul 2 '20 at 7:32
  • \$\begingroup\$ Have you write the correct name? \$\endgroup\$
    – Adrien G.
    Jul 2 '20 at 7:37
  • \$\begingroup\$ yep.. copy paste :) \$\endgroup\$
    – roni
    Jul 2 '20 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.