I’ve got a class that looks like this:
[System.Serializable]
public class Hex {
[SerializeField]
public int x = 0;
[SerializeField]
public int y = 0;
[SerializeField]
public int nation = 0;
[SerializeField]
public buildingType HexType = buildingType.None;
public Hex(int nation, int x, int y) {
this.nation = nation;
this.x = x;
this.y = y;
}
public Hex(int nation, int x, int y, buildingType t) {
this.nation = nation;
this.x = x;
this.y = y;
this.HexType = t;
}
}
buildingType is an Enum:
[System.Serializable]
public enum buildingType {
None = 0,
Base,
Mine,
Lumber,
Church,
Blacksmith,
Tower,
Ruin,
Field,
}
I need to send a jagged array of Hex objects through an RPC in Photon Unity Networking.
To do that with custom classes, you need to convert the object to a byte and then back when you receive it.
Here is what I’ve tried:
Sending
Debug.Log("init serialization", this);
byte[] mapToSend;
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream()) {
Debug.Log("begining serialization", this);
bf.Serialize(ms, map);
Debug.Log("reading bytes", this);
mapToSend = ms.ToArray();
}
Debug.Log("done serialization", this);
if (mapToSend == null) {
Debug.LogError("byte[] is null!", this);
}
photonView.RPC("SyncLevel", other, mapToSend, heightMap, materials);
Receiving
using (MemoryStream stream = new MemoryStream(bmap)) {
map = new BinaryFormatter().Deserialize(stream) as BoardGeneration.Hex[][];
}
My issues is that when the script goes it serialize the jagged array, it throws:
NullReferenceException: Object reference not set to an instance of an object
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.GetAssemblyNameId (System.String assembly) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:812)
Right after printing “begining serialization” to the console.
I know that map is not null, and there is not really any help for the specific error online that I could find.
Does anyone know what I’m doing wrong?