- Home /
2 Simple Questions. The Questions Are Comments In The Code
public class Example : MonoBehaviour
{
private Transform myTransform;
private Vector3 myPosition;
private void Start()
{
SetInitialReferences();
}
private void Update()
{
Debug.Log(myTransform.position.ToString()); //why does this update constantly?
Debug.Log(myPosition.ToString()); //this doesent update as I expected not like the other one
}
void SetInitialReferences()
{
myTransform = transform; //why is it faster to use myTransform later on than transform?
myPosition = transform.position;
}
myTransform = transform means that the your variable will point to the actual Transform Object because it is a reference type (as all classes). So it doesn't really copy it just points to the same Object in memory. As on if it is faster to cashe the reference rather picking it through the property transform if there is any difference it is trivial.
On the other hand myPosition = transform.position which is of Vector3 type copies the value so afterwards there are 2 different Objects in memory, thus any changes on the transform.position won't be reflected on the myPosition var as it is Value Type.
Search on google about reference types and value types there are alot of sites (including $$anonymous$$SDN official site) to find out more about them.
Cheers.
Sry for not seeing your answer earlier. Was the first time I used the forums and did not see that you commented as a reply ins$$anonymous$$d of an answer. Anyway thanks both of you!
Answer by MUG806 · Feb 08, 2018 at 02:09 PM
If I'm understanding the question, this comes down to the difference between objects and structs.
When you store "transform.position" in myPosition, you actually store a copy of the vector coordinates in the variable, because Vector3 is a struct. When you assign the object "transform" to myTransform, you are actually storing a reference to a transform object, which is just the way C# does things with objects. This way when you access myTransform.position, you are getting the original transform object, and then finding its current position, whereas with myPosition, you are just accessing a copy of the position as it was when you assigned the variable.
Can be tricky to get your head around at first, but C# just handles assigning objects differently to structs and basic data types like int and float.
Thx for the answer! Is clear now :) One last thing... in the method "SetInitialReferences" I set a reference of the transfrom to myTransform. I have heard that it is fast that way if want to access the transform over the reference ins$$anonymous$$d of the actual transfrom. Do you know why that is? And if yes, why is it that way?
$$anonymous$$ight be mistaken but I don't think that's the case. You may be getting mixed up with GetComponent() which you can use to access components attached to the game object. That method IS slow so the advice is to create a reference to the components you will be needing every frame just the once in initialisation.
I think you are mistaken. You can get a performance improvement from cacheing it. See eg https://forum.unity.com/threads/cache-transform-really-needed.356875/ It's no longer as slow as a GetComponent call, but still quicker to cache it yourself. Not so much as to make a huge difference, but I$$anonymous$$O it does little harm to get into the habit, at least for Transforms that are being accessed every frame.
Alright, TIL. I'd submit that as an answer cuz that is definition worth knowing.