Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

OnSerializeNetworkView() not working only for clients?

Discussion in 'Multiplayer' started by ChuckIes, Jun 11, 2015.

  1. ChuckIes

    ChuckIes

    Joined:
    May 18, 2015
    Posts:
    9
    So I have an odd issue. In my game, there are enemy mages that are controlled by AI. I do the AI thinking server-side (as clients may come up with different results) and push those results to the clients via OnSerializeNetworkView().

    I've placed print()'s everywhere, and it appears that OnSerializeNetworkView() is being called server-side, but not client-side. Here's my method:
    Code (CSharp):
    1. void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info) {
    2.         print ("serializing");
    3.  
    4.         // rb is our RigidBody2d
    5.         if(rb == null) {
    6.             print ("rigidbody null");
    7.             return;
    8.         }
    9.        
    10.         Vector3 syncPosition = Vector3.zero;
    11.         Vector3 syncVelocity = Vector3.zero;
    12.         int moveOrder = -1;
    13.         if(stream.isWriting) {
    14.             syncPosition = rb.position;
    15.             stream.Serialize(ref syncPosition);
    16.            
    17.             syncVelocity = rb.velocity;
    18.             stream.Serialize(ref syncVelocity);
    19.  
    20.             if(Network.isServer) {
    21.                 print ("depositing move order");
    22.                 moveOrder = (int)order;
    23.             }
    24.             stream.Serialize(ref moveOrder);
    25.         }
    26.         else {
    27.             stream.Serialize(ref syncPosition);
    28.             stream.Serialize(ref syncVelocity);
    29.             stream.Serialize(ref moveOrder);
    30.            
    31.             rb.position = syncPosition;
    32.             rb.velocity = syncVelocity;
    33.  
    34.             if(Network.isClient && moveOrder != -1) {
    35.                 order = (MoveOrder)moveOrder;
    36.                 print ("order recvd = " + order);
    37.             }
    38.         }
    39.     }
    The print()'s are all called server-side, but client-side, I don't get any - not even the first print("serializing");

    I have the script attached in the observed field in my Network View, and I've checked that both client and server side game objects' network view ID's are the same. The client doesn't lose connection. My mage enemy exists in the scene when I load it.

    I can't think of any other reason why it's sending but not receiving. Any help in resolving this issue would be appreciated.
     
  2. SeriousBusinessFace

    SeriousBusinessFace

    Joined:
    May 10, 2014
    Posts:
    127
  3. ChuckIes

    ChuckIes

    Joined:
    May 18, 2015
    Posts:
    9
    That problem looks similar, though not exactly.

    He wants clients to write data and the server to read data. I want the server to send data, but for some odd reason clients aren't receiving the data. They're not even calling OnSerializeNetworkView().
     
  4. darkmask58

    darkmask58

    Joined:
    May 9, 2015
    Posts:
    25
    Are you working in separated projects?
     
  5. ChuckIes

    ChuckIes

    Joined:
    May 18, 2015
    Posts:
    9
    Nope. They're in the same project.

    Though I think I've found a workaround. It seems like, for whatever reason, despite having the same network view IDs, the game treats them as separate entities. I changed the physical enemy in the scene with a "spawnpoint" game object that called Network.Instantiate() on the server on Awake(). That works fine.