How to store position in a variable and than edit only specific part of it(such as 'x')

So I am trying to store a whole position in a variable and than trying to edit only a specific part of it such as ‘x’ or ‘y’.
I am pretty new on Unity so im not that much familiar with its API so please excuse me if i say something weird or even if my script is completely wrong.

So here is how i am trying to solve my problem:

public Vector3 position = transform.position;
position.x = 1.1f;

this is what i get as an error:

Assets/Scripts/Player/Player.cs(10,27): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.transform'

this is what i have at line 10:

public Vector3 position = transform.position;

Well, you have two problems (one on each line :slight_smile: You can’t initialise a variable with a nonstatic field, and you can’t edit a single member of a struct.

Try this:

// First error is that you can't initialise the variable to transform.position here
public Vector3 position;

void Start() {

  // To prevent second (as yet unnoticed error), don't just assign the x member - 
  // Create a whole new struct instead.
  position = new Vector3(1.1f, transform.position.y, transform.position.z);