- Home /
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;
Answer by tanoshimi · Jan 23, 2015 at 07:38 PM
Well, you have two problems (one on each line :) 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);
}
Thanks for the help!
so when i am going to call a specific location i am going to use poisiton.y or position.x, right?
Oh and also, I tried to set my z coordinate to 0 zero if my TwoDimention boolean is true, but it didnt worked. No errors on my console.
if (TwoDimention == true)
{
position = new Vector3(transform.position.x, transform.position.y, 0);
}
if statement is inside of a Update void
Your answer
Follow this Question
Related Questions
Trouble setting up a vector 3. 1 Answer
Script to make Camera follow the player C# 3 Answers
Issue's With Positioning 0 Answers
Guided missile help c# 1 Answer
How to rotate an object to face the direction it's going? 1 Answer