- Home /
Duplicate Question - http://answers.unity3d.com/questions/topics/cs1612.html
error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable
Hi! I have this Code:
using UnityEngine;
using System.Collections;
public class GameSetup : MonoBehaviour
{
public Camera mainCam;
public BoxCollider2D topWall,bottomWall, leftWall,rightWall;
public Transform Player01,Player02;
// Update is called once per frame
void Update ()
{
topWall.size = new Vector2 (mainCam.ScreenToWorldPoint(new Vector3(Screen.width*2f,0f,0f)).x, 1f);
topWall.center = new Vector2 (0f, mainCam.ScreenToWorldPoint (new Vector3 ( 0f, Screen.height, 0f)).y + 0.5f);
bottomWall.size = new Vector2 (mainCam.ScreenToWorldPoint (new Vector3 (Screen.width * 2, 0f, 0f)).x, 1f);
bottomWall.center = new Vector2 (0f, mainCam.ScreenToWorldPoint (new Vector3( 0f, 0f, 0f)).y - 0.5f);
leftWall.size = new Vector2(1f, mainCam.ScreenToWorldPoint(new Vector3(0f, Screen.height*2f, 0f)).y);;
leftWall.center = new Vector2(mainCam.ScreenToWorldPoint(new Vector3(0f, 0f, 0f)).x - 0.5f, 0f);
rightWall.size = new Vector2(1f, mainCam.ScreenToWorldPoint(new Vector3(0f, Screen.height*2f, 0f)).y);
rightWall.center = new Vector2 (mainCam.ScreenToWorldPoint (new Vector3 (Screen.width, 0f, 0f)).x + 0.5f, 0f);
Player01.position.x = mainCam.ScreenToWorldPoint (new Vector3 (75f, 0f, 0f)).x;
Player02.position.x = mainCam.ScreenToWorldPoint (new Vector3 (Screen.width - 75f , 0f, 0f)).x;
}
}
and I get this error(twice): error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable
What to do? Thanks, Nir :)
While I admit that this question gets asked a lot, it's hardly appropriate to close it as a duplicate.
The problem is that you try to assign to Player01.position.x which you can't do. Create a new vector3 that contains the same as the player position, then assign the whole position, not just the x value
Follow this Question
Related Questions
Ending my game 3 Answers
Why is this player script not working 1 Answer
Debug.Log error 2 Answers
Tutorial script problem 1 Answer
GetComponent Picking up multiple scripts from differrent objects? 1 Answer