Unity 5.0.0b18 - OnSerializeNetworkView Bug

OnSerializeNetworkView doesn’t seem to be working properly. It works fine in 4.6 but not in 5 beta. The BitStream that it supplies is null. I couldn’t report it as a bug because the reporter kept failing.

Example code:
Make a GameObject then put a NetworkView on it that is set to observe this component which should also be on the object. Make a standalone build and run both it and the editor. You will see errors about the BitStream being null. If you do the same process but in Unity 4.6, it won’t be null.

using UnityEngine;

//Running this in the Unity Editor will result in initializing the server
//Running it as a Standalone build will result in connecting as client
//The server will bring up errors in the console letting you know that the BitStream is null for OnSerializeNetworkView
public class Connect : MonoBehaviour {

   bool tryToConnect = true;

   void Start() {
     if(Application.isEditor)
       Network.InitializeServer(16, 9001, true);
   }

   void Update() {
     if(!Application.isEditor) {
       if(tryToConnect)
         if(!Network.isClient && Network.peerType != NetworkPeerType.Connecting) {
           Network.Connect("127.0.0.1", 9001);
           tryToConnect = false;
         }
     }
   }

   void OnFailedToConnect() {
     tryToConnect = true;
   }

   void OnDisconnectedFromServer() {
     tryToConnect = true;
   }

   void OnSerializeNetworkView(BitStream stream) {
     if(stream == null)
       Debug.LogError("BitStream is null");
   }

   void OnGUI() {
     if(Network.isServer)
       GUILayout.Label("Server Online");
     else if(Network.isClient)
       GUILayout.Label("Client Online");
     else
       GUILayout.Label("Connecting");
   }
}
3 Likes

Sorry for my english.

I have similar situation. Bug in OnSerializeNetworkView exists 100%. I got my working project from unity 4.5.5 and in unity 5 it doesn’t work. If i use even clear method "void OnSerializeNetworkView(…) { } ", unity tells that i have error like null reference. When i delete "void OnSerializeNetworkView(…) { } " from my code, errors disappear. Fix it pls.

Fear no more, I had sent in a bug report and it looks like it’ll be fixed (don’t know when).
663681

Thanks for finding and reporting this bug @GibTreaty . It still exists in RC2 and I came across it with a slight variation: If you add the NetworkMessageInfo parameter to your OnSerializeNetworkView method then the method never actually gets called and instead you get a NullReferenceException in the console/log with no stacktrace whatsoever.

1 Like

I’m experiencing the same issue.

Same here. In rc1, rc2 - same bug. Lose 2 days on finding and fixing result = null. In 4.6 all works by 100%.

I didn’t see it mentioned in the release notes, but this appears to be fixed in RC3. Thank you to whoever fixed it! :slight_smile:

1 Like

Yep, it’s fixed!

Cool i’ll be updating to RC3 then!

1 Like