why does Matrix4x4.SetTRS() gives a different result than CombineInstance.transform.SetTRS()?

I ran into some problems with my combined Meshes getting scaled to zero using Mesh.CombineMeshes().
I was calling:

CombineInstance instance = new CombineInstance();
instance.transform.SetTRS(Vector3.zero, Quaternion.identity, Vector3.one);
Debug.Log(instance.transform);

and getting an output of:

[0,0,0,0,
 0,0,0,0,
 0,0,0,0,
 0,0,0,0]

However, I found that if I called:

Matrix4x4 matrix = new Matrix4x4();
matrix.SetTRS(Vector3.zero, Quaternion.identity, Vector3.one);
Debug.Log(matrix);

I would get the correct result of:

[1,0,0,0,
 0,1,0,0,
 0,0,1,0,
 0,0,0,1]

I could then store matrix in the transform on the CombineInstance and get the correct result. (My full code pulls from a TRS on a ScriptableObject. The “Vector3.zero, Quaternion.identity, Vector3.one” was for testing purposes.)

So I basically resolved my own problem.

My main question is: Why am I getting different results with what I assumed is the same function? Is this some sort of initialization problem on CombineInstance.transform? Is it a bug?

Matrix4x4 is a struct, so I’m guessing that when you call instance.transform it’ll return a copy, not a reference to the matrix. Try this!

CombineInstance instance = new CombineInstance();
Matrix4x4 mtx = instance.transform;
mtx.SetTRS(Vector3.zero, Quaternion.identity, Vector3.one);
instance.transform = mtx;
Debug.Log(instance.transform);

Or, since you’re setting it directly anyway:

    CombineInstance instance = new CombineInstance();
    instance.transform = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3.one);
    Debug.Log(instance.transform);

Or, since you’re setting it to the identity matrix anyway:

    CombineInstance instance = new CombineInstance();
    instance.transform = Matrix4x4.identity;
    Debug.Log(instance.transform);