- Home /
How do I change one value of a vector?
If I'm not mistaken, I've used code like this before:
transform.position.z = [value]; //but now it doesn't work.
Instead, it gives the following error: error CS1612: Cannot modify a value type return value of 'UnityEngine.Transform.position'. Consider storing the value in a temporary variable.
transform.position[3] = [value]; //doesn't work either.
I thought I found a way to get it to work, but it's incredibly longwinded, redundant, and ugly:
transform.position.Set(transform.position.x, transform.position.y, [value]); /*Gross, and edit: still doesn't work.*/
Is not being able to do the earlier ones a bug? Is there any better workaround?
(Making a temporary variable isn't fun - my code's now 21 lines instead of 9.)
Answer by WillNode · Apr 18, 2015 at 09:01 PM
you need to make a temporary variable first.
Vector3 position = transform.position;
position[2] = [value]; // the Z value
transform.position = position;
Well, that's what the error said, too, but I could have sworn there was a way that didn't take up three lines. Am I just remembering things wrong?
well, if you write this on JS it should work.
in C# you need to make temporary variable first.
In Unity script you don't have to declare a variable. You just go transform.position.x= 1.234;
C# one-line :
transform.position = new Vector3(transform.position.x, transform.position.y, zValue) ;
Answer by ripperdave · Aug 19, 2017 at 09:26 AM
Basically it is the same as the ideas before. You can create a static class in c#. You can use shorter class and method names as well. ChangeX to X etc.
public static class Utils {
public static Vector3 ChangeX(Vector3 v, float x)
{
return new Vector3(x, v.y, v.z);
}
public static Vector3 ChangeY(Vector3 v, float y)
{
return new Vector3(v.x, y, v.z);
}
public static Vector3 ChangeZ(Vector3 v, float z)
{
return new Vector3(v.x, v.y, z);
}
}
Usage in a different class.
public class Something: MonoBehaviour
{
void Update() {
transform.position = Utils.ChangeX(transform.position, 1); // change x
}
}
It can be shortened to:
transform.position = transform.position.X(1); //change x
Here's the class I use:
using UnityEngine;
/// <summary>
/// An auxiliary class containing methods that help change only one of the structure values.
/// </summary>
/// <example>
/// Usage:
/// <code>
/// gameObject.transform.localPosition = Change.Y(gameObject.transform.localPosition, f*0.10f);
/// </code>
/// or
/// <code>
/// gameObject.transform.localPosition = gameObject.transform.localPosition.Y(f*0.10f);
/// </code>
/// </example>
public static class Change {
public static Vector2 X(this Vector2 v, float x)
{
v.x = x;
return v;
}
public static Vector2 Y(this Vector2 v, float y)
{
v.y = y;
return v;
}
public static Vector3 X(this Vector3 v, float x)
{
v.x = x;
return v;
}
public static Vector3 Y(this Vector3 v, float y)
{
v.y = y;
return v;
}
public static Vector3 Z(this Vector3 v, float z)
{
v.z = z;
return v;
}
public static Color R(this Color c, float r)
{
c.r = r;
return c;
}
public static Color G(this Color c, float g)
{
c.g = g;
return c;
}
public static Color B(this Color c, float b)
{
c.b = b;
return c;
}
public static Color A(this Color c, float a)
{
c.a = a;
return c;
}
}
Usage:
gameObject.transform.localPosition = Change.Y(texture.transform.localPosition, f*0.10f);
or:
gameObject.transform.localPosition = texture.transform.localPosition.Y(f*0.10f);