I’m trying to make a simple thing with the Transform component of one of my GameObjects. To do so I added a script to it and added the following code:
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
private GameObject gameObject;
public void Start () {
this.gameObject = GameObject.Find("Main Camera");
}
public void Update () {
this.ManageUserInput();
}
private void ManageUserInput()
{
if (Input.GetKey(KeyCode.W))
{
Transform gameObjectTransform = this.gameObject.GetComponent<Transform>();
gameObjectTransform.position = new Vector3(
gameObjectTransform.position.x,
gameObjectTransform.position.y,
gameObjectTransform.position.z - 0.5);
}
}
}
As you can see it’s nothing that smart, but I whenever I run it I get the following error:
error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable
Can someone explain me why it doesn’t work?
Thanks in advance.