- Home /
error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable
Hi, I've been researching this error and how to fix it but I can't seem to figure out how. If you could point out what's wrong I would very much appreciate it.
using UnityEngine;
using System.Collections;
public class CameraTrack : MonoBehaviour {
public float xOffset;
public float yOffset;
public GameObject player;
public Vector3 playerPos;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
playerPos = player.transform.position;
transform.position.x = playerPos.x + xOffset;
transform.position.y = playerPos.y + yOffset;
}
}
The error is at lines 17 and 18. Thanks in advanced!
I am getting the same error, even though I am using a temporary float. Can someone please help? What am I doing wrong?
void StopBall ()
{
if (rigidbody.velocity.x <= stopVelocityThreshhold.x && rigidbody.velocity.x != 0f) {
float tempX = 0;
//Error occurs here
rigidbody.velocity.x = tempX;
Debug.Log("X Velocity Stopped");
}
if (rigidbody.velocity.z <= stopVelocityThreshhold.z && rigidbody.velocity.z != 0f) {
float tempZ = 0;
//Error occurs here
rigidbody.velocity.z = tempZ;
Debug.Log("Z Velocity Stopped");
}
if (rigidbody.velocity == Vector3.zero) {
CancelInvoke();
}
}
You must not use a "temporary float", but a temporary "Vector3".
$$anonymous$$oreover, do not try to compare two vectors (with floating values) using the egality symbol ==
Compare the distance ins$$anonymous$$d.
void StopBall ()
{
Vector3 velocity = rigidbody.velocity ;
if (rigidbody.velocity.x <= stopVelocityThreshhold.x && rigidbody.velocity.x != 0f) {
velocity.x = 0;
}
if (rigidbody.velocity.z <= stopVelocityThreshhold.z && rigidbody.velocity.z != 0f) {
velocity.z = 0;
}
if ( Vector3.Distance( rigidbody.velocity, Vector3.zero ) < 0.01 ) {
CancelInvoke();
}
rigidbody.velocity = velocity ;
}
Answer by JasonBricco · May 24, 2014 at 08:31 PM
You can't set things to transform.position.x or transform.position.y. You need to do something like:
Vector3 tempPos = transform.position;
tempPos.x += xOffset;
tempPos.y += yOffset;
transform.position = tempPos;
PS: this isn't related to the question, but do consider caching the 'transform' on start, because I see you're using it in update every frame. It has a hidden GetComponent call and is a little performance intensive. So make a variable such as: Transform myTransform; and then at start say myTransform = transform;
Then you can refer to the transform as myTransform in update. Just a side note.
Answer by Jerryle · Mar 16, 2016 at 05:44 PM
SpriteState s= new SpriteState(); s.highlightedSprite = Instantiate(Resources.Load("image")); tf.GetComponent().spriteState = s;
Uhm, this question:
is 2 years old
has an accepted answer with 4 upvotes which is correct
is about a very common gotcha and has been answered hundreds of time
In addition your answer:
doesn't explain anything,
contains unformatted code
has nothing to do with the actual question.
If you wanted to show that SpriteState has the same problem as it's also a value type, you could have posted a question about that and in addition post an answer that explains that case. Your answer just "necro'd" this question by posting a very poor answer that isn't related to the question...
Your answer
![](https://koobas.hobune.stream/wayback/20220613145815im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
An NPC "moonwalks" 2 Answers
Unity + XCode Problem (really wierd) 0 Answers
Instantiate object in C# 1 Answer
Need help calling a script to another keep getting errors 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers