I am following this example to create a scriptable tile: Unity - Manual: Scriptable Tile example
Everything seems to work fine except the rotation of the tile.
In the GetTileDate method I have this:
Debug.Log("Index; " + mask + " for " + location);
tileData.sprite = m_Sprites[index];
tileData.color = Color.white;
Quaternion q = GetRotation((byte)mask);
tileData.transform.SetTRS(Vector3.zero, q, Vector3.one);
//tileData.flags = TileFlags.LockTransform;
tileData.colliderType = ColliderType.None;
The console output shows that I do ge a rotation from the GetRotation. It is usually (0,0,90f) however setting this using the SetTRS doesn’t have any affect…
The tileData.transform.rotation always seem to be (0,0,0).
What am I doing wrong here?
edit: I’ve done some testing and it looks like it’s not just the rotation but everything…
tileData.transform.SetTRS(Vector3.one, GetRotation((byte)mask), Vector3.one*2);
I’ve set this as a test and nothing got applied…
TileData.transform is a property and Matrix4x4 is a value type. That means your code will execute the “getter” of the property which will return a copy of the matrix. You then modify that copy. However this will have no effect on the actual internal matrix.
To change properties of value types you have to invoke the setter of the property. So you have to assign a Matrix4x4 value to the property.
tileData.transform = Matrix4x4.TRS(Vector3.one, GetRotation((byte)mask), Vector3.one*2);
Note that this is true for any kind or property of a value type. The same holds true for the normal Transform.position for example. You can’t do
transform.position.x = 3;
You have to do
Vector3 pos = transform.position; // call getter to get a copy
pos.x = 3;
transform.position = pos; // call the setter to actually update the position.
I did some more testing and it appears that the
tileData.flags = TileFlags.LockTransform;
Is the crucial part. In my code above I commented out. I did this while testing some things out. After I uncommented it, everything was working just fine.
The documentation is a bit scares when it comes to this property so it wasn’t actually clear to me…