1
\$\begingroup\$

I have this prefab with this transform:

enter image description here

And then using this code to create a clone (the prefab is assigned to a public property of a script, which I assign using the editor, which is the e.Weapon):

Instantiate(e.Weapon, new Vector3(0f, 0f, 0f), Quaternion.identity, rightHand.transform);

I am parenting it to my character's right hand. But whenever the clone gets created, it has different position than the one I have in my code. It appears like this:

enter image description here

Any idea why this is happening?

\$\endgroup\$
  • \$\begingroup\$ is your object child of another one? \$\endgroup\$ – virtouso Oct 15 at 5:39
  • \$\begingroup\$ @virtouso: yes, it is \$\endgroup\$ – g_b Oct 15 at 8:30
2
\$\begingroup\$

First guess: It sounds like you have a parent gameObject and you instantiate it in world space [0, 0, 0]. Then your position would of course be something else than [0, 0, 0] because the inspector would show the local position to its parent, not world position. The Instantiate method has overloads with a Boolean for spawning objects in world position. However I'd assume the Instantiate method would use local space for the vector anyway, given that it allows you to assign a parent. Have a look into that.

Another guess: You set the position somewhere else by code upon Awake() or Start() of a script on the instantiated item. In this case look into all scripts attached to it.

\$\endgroup\$
  • 1
    \$\begingroup\$ Using the one with the boolean overload worked when I gave it a false value. I didn't use it at first because I thought it was false by default. \$\endgroup\$ – g_b Oct 15 at 8:37

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.