- Home /
Question regarding this line of code
This line of code is in a book, but it dosen't work
transform.position.x += .2;
In the book all it is is a
void Update() {
transform.position.x += .2;
}
This should move the current object +.2 each update for x. Thanks
Also the error is "Assets/Paddle.cs(16,19): error CS1612: Cannot modify the return value of `UnityEngine.Transform.position' because it is not a variable" which i undersatnd that transform.position.x returns a value and cannot be set. But this book says it and it's a unity game dev book.
Can you post your full script and explain your setup? With what you've provided, we don't even know when it happens!
Cheers, the error made it much easier to figure out what was going on! :)
Answer by · Oct 29, 2010 at 06:08 AM
In UnityScript (~JavaScript), that code should work fine. In C#, however, you have to assign a new Vector3 to the position.
transform.position = transform.position + new Vector3 (.2f, 0, 0);
Alternatively, you can use Transform.Translate to translate the position by the specified amount. i.e.
transform.Translate(0.2f, 0, 0);
Your answer
Follow this Question
Related Questions
Is there a performance difference between Unity's Javascript and C#? 6 Answers
Script translation 2 Answers
How should I decide if I should use C#, JavaScript (UnityScript) or Boo for my project? 14 Answers
What are the Syntax Differences in C# and Javascript? 7 Answers
Can I declare properties in JS? 1 Answer