Serialize a LineRenderer

I’m trying to send a LineRenderer over the network with RPC calls. Since there is no way to get information out of a line renderer after it is created, I can’t just get the points and do something clever like converting them to a byte array and sending that. I thought about using an XmlSerializer to convert the data to a string and send that, but I’m getting an error when attempting to serialize:

System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(LineRenderer));

The above throws an error:

InvalidOperationException: To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object) at all levels of their inheritance hierarchy. UnityEngine.Transform does not implement Add(System.Object).

What I don’t understand is: at no point does LineRenderer inherit from UnityEngine.Transform. If you follow the inheritance it goes LineRenderer->Renderer->Component->UnityEngine.Object. None of these appear to implement the IEnumerable interface.

Does anyone wiser than I in the ways of C# know what’s going on here?

As @Luiz said:

The compiler is complaining about the
transform property from LineRenderer
(inherited from Component). To use
XmlSerializer all public properties
and fields must be serializable, so I
think you will need to do it by hand.

That solves it - I wasn’t understanding what Unity was trying to tell me. It was having an issue with the inheritance of a variable within the class it was trying to serialize, not the class itself.

Unfortunately, there is no way to serialize a LineRenderer by hand, since you can’t get at any of the LineRenderer data (there are no public variables or methods that return variables). I was hoping that I could get around that issue by serializing, but it appears not.

There no private members to read the data either. But you could create a class that holds the data needed to recreate the LineRenderer: a list of points, two colors and two width values. Something like:

public class LineRendererData
  public List<Vector3> points;
  public float startWidth, endWidth;
  public Color startColor, endColor;
  public void UpdateLineRenderer(LineRenderer renderer)
    for(int i = 0; i < points.Count; ++i)
      renderer.SetPosition(i, points*);*

renderer.SetColors(startColor, endColor);
renderer.SetWidth(startWidth, endWidth);
If you need to create the points on the Editor you could write a script that has a public LineRendererData field, executes on edit mode and calls UpdateLineRenderer when the data was changed.