GameObject position set

I want to set the position of a GameOject in a C# script attached to another GameObject.

I define “public GameObject airplane” in the script and drag the gameobject in Hierarchy to the definition in Inspector.

In the “start” function, I have “airplane.transform.position.Set(x,y,z)”. But the gameObject is still at the location I set in Inspector instead of the location (x,y,z) in the script.

Why?

This solution provides an answer to the question why “airplane.transform.position.Set(x,y,z)” does not work

Transform.position is a Vector3 which has a Set function, however…

The problem is that transform.position
(or .rotation, .velocity, …) is a
property. Properties aren’t variables.
They consists of a set and get method
which will be executed whenever you
read or write to the property. Since
Vector3 is a value type whenever pass
a Vector3 around you will copy it’s
content. When you do
transform.position.XXXX

You will get a copy of the Vector3 so
when you change something on the
struct you just change the local copy
of it. To actually change the position
you have to “invoke” the setter method
by assigning a Vector3 value to
transform.position.

Source: @Bunny83

Solution is to use either:

  1. transform.position = new Vector3(x,y,z); (which after another test does work, sorry to @alucardj for implying otherwise)

  2. Directly in the Inspector instead of Start

  3. Make the object private (or public but non serializable) and then assign via Find in the Start function. Then you would set the position in Start also; the Inspector would not be able to override