- Home /
transform.position
in c# the following code:
transform.position.y = 7.0f; give me this error:
Assets/Scripts/enemy.cs(19,35): error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable.
A hand please, i don't know what's wrong.
Answer by aldonaletto · Nov 25, 2011 at 01:27 AM
C# and Boo don't accept changes to single components of properties: you must copy to a variable, change the component and save it back:
Vector3 temp = transform.position; // copy to an auxiliary variable...
temp.y = 7.0f; // modify the component you want in the variable...
transform.position = temp; // and save the modified value
Thanks aldo, but after a while looking for an answer, i found a similar post. I'm just starting to use unity but it's looks like c# it's more complicated than javascript.
Yes, C# is way more complicated than javascript - too complicated for a script language, in my opinion. I use javascript 99% of time: it takes care of many annoying things for us, avoiding lots of typing. But learning C# is useful, because some things can only be done in this language - like declaring DLL external functions, for instance.
I am the exact opposite, I almost exclusively use C#, even though it is more difficult, it runs faster than javascript, and I noticed that it can handle arrays when C# can't, but you can always use lists in their place.
I don't believe C# runs faster than JS: both compile to very similar IL code. Install ILSpy in your machine and take a look at the IL code generated. You can also decompile it to C#, even if it was originally written in JS: the decompiled code is basically the same. About the Array class: it's very inefficient, thus you're better off without it - the List class is a much better solution for dynamic arrays.
Thanks aldo, i tried your solution and even if it so simple, it works perfectly. I congratulate for your answers, always so useful! Thank you!
Answer by Stardog · Aug 04, 2012 at 07:38 PM
Or you can do:
transform.position = new Vector3(transform.position.x, 7f, transform.position.z);
Answer by MAYALS18 · May 30, 2018 at 07:51 AM
To make your object rotate, you need to make a variable which shows that and then put it into a Vector3.
using UnityEngine
public class Example : MonoBehaiviour {
private float axisY = 7.0f;
void exampleClass () {
transform.position = new Vector3 (transform.position.x, axisY, transform.position.z);
}
}