- Home /
Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable
I am following the 2d pong tutorial by Brackeys found here: https://www.youtube.com/playlist?list=PLPV2KyIb3jR4_IYZY2V0G3IUYcx1zZkJe
He uses JS and I prefer C#. I am just unsure how to convert this part.
Player01.position.x = mainCam.ScreenToWorldPoint (new Vector3 (75f, 0f, 0f)).x;;
Player02.position.x = mainCam.ScreenToWorldPoint (new Vector3 (Screen.width - 75f, 0f, 0f)).x;
Any help would be appreciated.
Answer by Maui-M · Feb 17, 2014 at 09:40 PM
Vector3 temp = mainCam.ScreenToWorldPoint (new Vector3 (75f, 0f, 0f))
Player01.position.x = temp.x;
temp = mainCam.ScreenToWorldPoint (new Vector3 (Screen.width - 75f, 0f, 0f));
Player02.position.x = temp.x;
Answer by BeginnerGuy · Feb 17, 2014 at 10:07 PM
so mainCam.ScreenToWorldPoint (new Vector3 (75f, 0f, 0f)) is a Vector 3. And you are just making a new Vector3 named temp and storing that information to it.
Then Player01.position.x = temp.x means its getting the Vector3 x coord
Thank you I understand that now.
Answer by Durayel · Feb 17, 2014 at 10:05 PM
You need to read the entire position, modify just the applicable coordinates and then reassign the whole new position eg.
Vector3 position = Player01.position; position.x = mainCam.ScreenToWorldPoint (new Vector3 (75f, 0f, 0f)).x; Player01.position = position;
Your answer
![](https://koobas.hobune.stream/wayback/20220613133841im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Making a rigidbody cube snap 90 deg at a time 1 Answer
How can apply the climb animation to player? 2 Answers
java to C# 2 Answers
Rewriting script from JavaScript to C# 1 Answer
How to move the object at the center of the grid cell? 0 Answers