GameObject position.Set() not working

I’m trying to instantiate a clone of an object at position X, then move the original object to position Y; however, the Position.Set() doesn’t seem to do anything. Both objects stay at the position X, even though setting gravity and velocity work fine.

The pieces of my code in question:

(The script function Update() on the original object)

void Update() 
    {
        if (isFree == true && isActive == true)
        {
            if (Input.GetMouseButtonDown(0))
            {
                GameObject.Find("LaunchPoint").GetComponent<Loader>().Load();

                transform.position.Set(Anchor.transform.position.x, Anchor.transform.position.y, Anchor.transform.position.z);
                rigidbody.velocity.Set(0, 0, 0);
                rigidbody.useGravity = false;
                isFree = false;

                GameObject.Find("Main Camera").GetComponent<FollowObject>().MoveToPos(transform.position);
            }
        }
    }

Anchor is another gameobject in the scene that doesn’t move.

(The code on the object that does the ‘reloading’ (the copy bit)):

public class Loader : MonoBehaviour {

public GameObject target;

public void Load() 
{
    GameObject newTarget = (GameObject)Instantiate(target);
    newTarget.transform.rotation.Set(target.transform.rotation.x, target.transform.rotation.y, target.transform.rotation.z, target.transform.rotation.w);
    newTarget.rigidbody.velocity.Set(target.rigidbody.velocity.x, target.rigidbody.velocity.y, target.rigidbody.velocity.z);
    newTarget.GetComponent<FollowMouse>().enabled = false;
}

// Update is called once per frame
void Update()
{
    target = GameObject.Find(target.name);
}

}

I can’t understand why the position of the original object (first bit of code) doesn’t move the object. I’ve also tried it with number values (0,0,0) and it doesn’t do anything either, when everything else appears to work properly.

I’ve encountered this issue as well and whilst I can’t explain why exactly I can tell you how to work around it. Do this:

Vector3 newPos = new Vector3(Anchor.transform.position.x, Anchor.transform.position.y, Anchor.transform.position.z);
transform.position = newPos;

Hope that helps

Edit: transform.position returns a copy , that’s why Set() doesn’t work. You’d think the compiler would issue a warning.

In your gameobject, you should have a script that inherits from MonoBehavior. In there, (in a function; update or start or whatever) you can access the transform subclass with this code:

transform.position = new Vector3 (x, y, z);

You can also access the individual values (x, y, z) like this:

transform.position.x

I’m not sure if you can directly modify those, but the best way to learn is to try it out for yourself.

I know this is a super old question, but I figure this might help someone else.