Invalid Cast Error with Ros Subscriber

Hello,

I publish and subscribe to a ROS topic in Unity with my own msg and srv. I followed this tutorial: Unity-Robotics-Hub/tutorials/ros_unity_integration at main · Unity-Technologies/Unity-Robotics-Hub · GitHub
Publishing works without issues but the subscriber gets this error: I still receive the message and can print it.

InvalidCastException: Specified cast is not valid.
(wrapper castclass) System.Object.__castclass_with_cache(object,intptr,intptr)
Unity.Robotics.ROSTCPConnector.ROSConnection+<>c__DisplayClass78_01[T].<Subscribe>b__0 (Unity.Robotics.ROSTCPConnector.MessageGeneration.Message msg) (at Library/PackageCache/com.unity.robotics.ros-tcp-connector@c27f00c6cf/Runtime/TcpConnector/ROSConnection.cs:194) Unity.Robotics.ROSTCPConnector.RosTopicState+<>c__DisplayClass56_0.<OnMessageReceived>b__0 (System.Action1[T] item) (at Library/PackageCache/com.unity.robotics.ros-tcp-connector@c27f00c6cf/Runtime/TcpConnector/RosTopicState.cs:94)
System.Collections.Generic.List1[T].ForEach (System.Action1[T] action) (at :0)
Unity.Robotics.ROSTCPConnector.RosTopicState.OnMessageReceived (System.Byte[ ] data) (at Library/PackageCache/com.unity.robotics.ros-tcp-connector@c27f00c6cf/Runtime/TcpConnector/RosTopicState.cs:94)
Unity.Robotics.ROSTCPConnector.ROSConnection.Update () (at Library/PackageCache/com.unity.robotics.ros-tcp-connector@c27f00c6cf/Runtime/TcpConnector/ROSConnection.cs:600)
UnityEngine.Debug:LogException(Exception)
Unity.Robotics.ROSTCPConnector.ROSConnection:Update() (at Library/PackageCache/com.unity.robotics.ros-tcp-connector@c27f00c6cf/Runtime/TcpConnector/ROSConnection.cs:605)

My subscriber:

void Start()
{
//subscribe to topic name and call funtion Print
ROSConnection.GetOrCreateInstance().Subscribe(topicName, Print);
}

//msg is the data from the publisher
void Print(DigitalTwinMsg msg)
{
Debug.Log(msg);
}

Hmm, it’s not obvious what’s happening here. Is it just your custom message that’s failing in this way? Are you able to subscribe to other message types?

Yes, other custom messages worked, although I only tried with a single string as a message, apart from the ones from the tutorial.

The error was caused by a similar but different .srv file. I deleted it and rebuild my services and messages and its now working fine.