Strange Variables in OnSerializeNetworkView?

The data I’m receiving is not the data I should be receiving using OnSerializeNetworkView. Is there anything wrong with this code or anything that could end up in weird variables?

function OnSerializeNetworkView (stream : BitStream, msg : NetworkMessageInfo)
{
 
 var pos = transform.position;
 var rot = transform.rotation;
 
 var newPos = pos;
 var newRot = rot;
 
 if (stream.isWriting) {
        
        
        if (isactive)
        {
         pos = transform.position;
         rot = transform.rotation;
         newPos = pos;
         newRot = rot;
         Debug.Log("isactive: " + newPos);
         stream.Serialize(newPos);
         stream.Serialize(newRot);
        }
        
        
    } else {
     
       
        
        if (!isactive)
        {
     stream.Serialize(newPos);
         stream.Serialize(newRot);
         Debug.Log("Recieve " + newPos);
         pos = newPos;
         rot = newRot;
        }
        
        
        
    }
    
    if (!isactive)
    {
     transform.position = pos;
     transform.rotation = rot;
     Debug.Log("Transform equals: " + pos);
    }
    
}

Thanks!

I’ve read most of the comments here and i would like to add some general hints:

  • When using the network, make sure your application runs in background. Otherwise the server (or client) won’t respond to any package sent when the application doesn’t has the focus. That’s the most common mistake. You can also set this via scripting at runtime.
  • “Automatic syncing”, either by setting a Transform as observed object, or by using OnSerializeNetworkView, only the object owner can send any data and all others can just receive the updates. Owner referes to the NetworkViewID owner. This specifies who owns the object with that ViewID. The owner can’t be specified manually. Whenever someone creates a new ViewID, this player will be the owner. Network.Instantiate creates a ViewID for all NetworkViews that are within the object and it takes care of sending them to all others so they are set properly on all peers. So the player that calls Network.Instantiate will own the object and he’s the only one who can move the object.
  • When using syncing with an observed Transform, Unity will only send an update when the position / rotation has changed. When using OnSerializeNetworkView you can send any arbitrary data, so deltaCompression will probably be disabled.
  • The SendRate (via scripting) specifies the update interval how often a package is send and therefore how often OnSerializeNetworkView will be called.
  • If you want to disable syncing of a certain NetworkView, just set the stateSynchronization property to Off. In this case, no updates are send.

It’s vital that all receiving peers deserialize exactly the same data that you send. You change the data you’re sending depending on your isactive variable. When this variable is changing on the sending peer, the receiving peer doesn’t know about that.

One way is to send the isactive first and decide on that what and if additional data will follow.

function OnSerializeNetworkView (stream : BitStream, msg : NetworkMessageInfo)
{
    var pos = transform.position;
    var rot = transform.rotation;
    var active = isactive;
    
    stream.Serialize(active);  // always send / receive the active flag
    if (active)
    {
         stream.Serialize(pos); // only send / receive pos / rot when active
         stream.Serialize(rot);
    }
    
    if (active && stream.isReading)
    {
        transform.position = pos;
        transform.rotation = rot;
        //isactive = active;  // if you need it also on the other peers.
    }
}

if this happens on two different clients connected to server, then pay attention what happened on server. server in this case is not just resends data, it applies to instantiated object, then reads it again from object and sends to other players. so if you have some differences in scene that could change position of object - that’s why your issue happens.