Reputation: 55
I am new to Unity. The code is in 2d and c#. I am trying to make it that when the player goes below they appear above and when the player goes above they appear below. Here is the error I get:
Assets/Scripts/Player.cs(23,43): error CS1612: Cannot modify a value type return value of UnityEngine.Transform.position'. Consider storing the value in a temporary variable
Assets/Scripts/Player.cs(27,43): error CS1612: Cannot modify a value type return value of
UnityEngine.Transform.position'. Consider storing the value in a temporary variable
Here is my code that I am having problems with:
if (transform.position.y > 5.5f)
{
transform.position.y=-10f;
}
if (transform.position.y < -10.5f)
{
transform.position.y=5;
}
P.S. I know about Unity answers, I asked my question there about 10 hours ago and moderators still haven't approved yet. That is why I am here.
Upvotes: 0
Views: 97
Reputation: 350
You can't directly use transform.position.y to set a value for it. Instead Unity is asking that you use some temporary value or simply write as following -
if (transform.position.y > 5.5f)
{
transform.position = new Vector3(transform.position.x, -10.0f, transform.position.z);
}
if (transform.position.y < -10.5f)
{
transform.position = new Vector3(transform.position.x, 5.0f, transform.position.z);
}
Or if you want to use temporary variable, use this -
if (transform.position.y > 5.5f)
{
Vector3 newPosition = new Vector3(transform.position.x, -10.0f, transform.position.z);
transform.position = newPosition;
}
if (transform.position.y < -10.5f)
{
Vector3 newPosition = new Vector3(transform.position.x, 5.0f, transform.position.z);
transform.position = newPosition;
}
Both are same. But for convenience, i would go for first one here in your case.
Upvotes: 2