In Unity3D setting the Quaternion of a GameObject directly, without constructing a new object every time

As you can see in the documentation Quaternion is a struct, not a reference type.

This means:

  • transform.rotation returns a copy of the current rotation of your object. Thus you cannot modify it by calling transform.rotation.Set(x, y, z, w);. You have to use an assignment call.
  • You will not improve performance by caching the rotation in a member field, since the assignement will copy it anyway and your local Quaternion is allocated on the stack.

If you are afraid of performance (which you shouldn’t be just because of the Quaternion construction), I would suggest you cache transform in a member, since Unity’s transform member calls this.gameObject.GetComponent<Transform>() every time you use it, which is a rather expensive operation. Though for true optimization we need to know your exact code.


public class MyClass
    // to be used instead of Unity's transform member
    private Transform myTransform;
    private void Awake()
        // make expensive call only once during Awake
        myTransform = transform;

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top