- Home /
getting the position of player for the minimap to follow
using UnityEngine; using System.Collections;
public class MapFollow : MonoBehaviour {
[SerializeField] public Transform target;
void Update ()
{
gameObject.transform.position.z = target.transform.position.z;
gameObject.transform.position.x = target.transform.position.x;
}
}
This is the error:
Assets/IBN/MapFollow.cs(15,38): error CS1612: Cannot modify a value type return value of UnityEngine.Transform.position'. Consider storing the value in a temporary variable Assets/IBN/MapFollow.cs(16,38): error CS1612: Cannot modify a value type return value of
UnityEngine.Transform.position'. Consider storing the value in a temporary variable
Answer by getyour411 · Sep 05, 2015 at 01:10 AM
A C# gotcha; create a new Vector3(someX, someY, someZ)
assign gameObject.transform.position to that new Vector3; you can't do the x= new x, y=new y z=new z stuff in C#
Just to clarify a little. Position on a transform is a property with a getter setter. In c# you can't modify a properties fields of a value type. Vector3 in unity is a struct which is a value type. So what @getyour411 is saying is create a new vector3 and set the position to that.
transform.position = new Vector3(target.position.x, transform.position.y, target.position.z);
Your answer
Follow this Question
Related Questions
how are Mathf.SmoothDamp and Mathf.SmoothStep different 1 Answer
C# locking Main Camera's Rotations 1 Answer
Distribute terrain in zones 3 Answers
GameObject transform relative camera 0 Answers
Multiple Cars not working 1 Answer