- Home /
Cannot modify a value type return value of `UnityEngine.Transform.position'. Can anyone help ?
Hi. When I save this script
using UnityEngine;
using System.Collections;
public class Camera_Controll : MonoBehaviour {
float margin = 1.5f; // space between screen border and nearest fighter
private float z0 = 0; // coord z of the fighters plane
private float zCam; // camera distance to the fighters plane
private float wScene; // scene width
private Transform f1; // fighter1 transform
private Transform f2; // fighter2 transform
private float xL; // left screen X coordinate
private float xR; // right screen X coordinate
void calcScreen ( Transform p1 , Transform p2 ){
// Calculates the xL and xR screen coordinates
if (p1.position.x<p2.position.x){
xL = p1.position.x-margin;
xR = p2.position.x+margin;
} else {
xL = p2.position.x-margin;
xR = p1.position.x+margin;
}
}
void Start (){
// find references to the fighters
f1 = GameObject.Find("Player1").transform;
f2 = GameObject.Find("Com").transform;
// initializes scene size and camera distance
calcScreen(f1,f2);
wScene = xR-xL;
zCam = transform.position.z-z0;
}
void Update (){
calcScreen(f1,f2);
float width = xR-xL;
if (width>wScene){ // if fighters too far adjust camera distance
transform.position.z = zCam*width/wScene+z0;
}
// centers the camera
transform.position.x = (xR+xL)/2;
}
}
I have error like this
Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable
Can anyone help ?
Answer by Owen-Reynolds · Jan 27, 2013 at 03:22 PM
C# has a (stupid) rule that you can only change certain compound variables all at once. The hint "Consider storing the value in a temporary variable" is exactly the fix.
Would have been nice if you wrote the line number, but this line: transform.position.z = zCam*width/wScene+z0;
is it. You have to assign transform.position all at once. An easy way is:
Vector3 TP = transform.position; TP.z= ...; transform.position = TP;
That seems like a giant pain, but in practice, you often want to copy trans.pos into something short like TP before using it a lot (and it runs a tiny bit faster.)
Thanks, now I don't have any error but script don't work... I have same script in JS and it work:
#pragma strict
var margin:float = 1.5; // space between screen border and nearest fighter
private var z0:float = 0; // coord z of the fighters plane
private var zCam:float; // camera distance to the fighters plane
private var wScene:float; // scene width
private var f1:Transform; // fighter1 transform
private var f2:Transform; // fighter2 transform
private var xL:float; // left screen X coordinate
private var xR:float; // right screen X coordinate
function calcScreen(p1:Transform, p2:Transform){
// Calculates the xL and xR screen coordinates
if (p1.position.x<p2.position.x){
xL = p1.position.x-margin;
xR = p2.position.x+margin;
} else {
xL = p2.position.x-margin;
xR = p1.position.x+margin;
}
}
function Start(){
// find references to the fighters
f1 = GameObject.Find("Player1").transform;
f2 = GameObject.Find("Com").transform;
// initializes scene size and camera distance
calcScreen(f1,f2);
wScene = xR-xL;
zCam = transform.position.z-z0;
}
function Update(){
calcScreen(f1,f2);
var width:float = xR-xL;
if (width>wScene){ // if fighters too far adjust camera distance
transform.position.z = zCam*width/wScene+z0;
}
// centers the camera
transform.position.x = (xR+xL)/2;
}
But I'm going to write it in C#...