When referencing a transform.position for a function, is there a way to set a variable so that the position is the position of another object? So that if something like this were set up, the object would move towards ObjectA? Thanks.
var target =ObjectA.Position
function Update (){
transform.position = target
}
Close:
var target : Transform = ObjectA;
function Update (){
transform.position = target.position;
}
Just realized, might be worthwhile to offer some explaination:
When working with value-types (i.e. “primitive types”) such as integers, strings, booleans, and Vector3s, these are “passed by value”. That is, every time you work with them, it creates a copy of it instead of referencing the original. (by the way, don’t be concerned about performance with it making copies all the time: it’s actually purposely designed this way to be very fast)
When working with reference-types (e.g., your ObjectA, transforms, textures) these are “passed by reference”. Instead of copying all the data, you just pass a reference to that original object. That way, as that object’s properties are changed, you can check to see those changes.
In your original code, you has looked at the value of ObjectA’s position at the time when you assigned it. But Vector3s are value-types, so you only ever held the position of ObjectA when it was first assigned. As ObjectA changed, your “target” variable would not update accordingly. But by switching it to hold ObjectA instead, because Transform is reference-type, you can always lookup its various properties as they change.