UNET NetIDs for Scene Objects Mismatched Between Client and Server
While working on a multiplayer project, I've encountered a rather bizarre issue. My online scene includes a few objects with NetworkIdentities. From the documentation, it seems that NetworkIdentities in the scene at compile time will be disabled on scene load at runtime, then enabled via NetworkServer.SpawnObjects. This does occur, but for some reason, the NetworkInstanceIds of some objects become swapped with others which leads the client to think object A is object B. That is, the server assigns the NetId of 1 to object A and 2 to B, but the client sees it reversed, which puts objects in the wrong location. Weirder still, it seems to set object A's position to B's position, but keep object A's rotation, so it's not a straight transform mix-up.
I've reproduced this issue in both 5.6.2f1 and 5.6.3f1. I couldn't find anything in the docs or elsewhere online. Help would be greatly appreciated!
Answer by Malace · Aug 22, 2017 at 06:09 AM
Truth is UNET does not keep accurate track of network ID's from what I can tell. While your server player likely gets the correct A to B correlation. Your client generates different ID's for those same objects. It might likely be due to lag delay in the clients packets. Or something causing the stack trace to load object B before A. Thus assigning its first available netid to the wrong object. The same thing can happen when generating Net ID's for players. So making a distinction there can cause the same headache.
You're case may be easier to solve. in your code for your object a and b. Make a system to track each instance. Static int, or increment a item ID of your own on spawn. Make a return method for said ID. After that you can foreach the return of each object. IE:
[command]
void CmdMySpawnCommand()
{
GameObject myObjectToSpawn = (GameObject)Instantiate(myObjectToSpawnPrefab, vector3.zero, Quanternion.Identity);//<This is where the server object is made.
//instantiate your objects in batch with a tag to identify them later.
//you pretty much can only change basic component values of the object here. Set Int, float, string, ect. No object refrences, transforms/vector 3's ect.
NetworkServer.Spawn(myObjectToSpawn);//<This is where a client creates the object.
RpcMySpawnRPC();//call your RPC command and setup all objects regardless of server vs client. This should make any changes you need to the object.
}
[clientRPC]
void RpcMySpawnRPC()
{
foreach (GameObject a in GameObject.FindObjectsWithTag("MyObjectTag"))//< you could use this to build a list of the objects or just straight to their custom script components
{
if (a.GetComponent<MyObjectScript>().ReturnMYObjectIDMETHOD() == 0)
{
///this is object A set its location. To iterate through all of these quickly you will need to store the inteded x, y, and z of the object. as well as its rotation vector x, y ,z. internally if you want to set it prior to this point via double foreach loop.
}
}
}
Without code that its my best guess as I ran into something similar with players. NetworkIdentity as I said doesn't seem to sync correctly. Just got past this myself so if you need more info just ask.
Your answer
Follow this Question
Related Questions
How to find local player gameobject? c# 3 Answers
How to decide an asynchronous multiplayer solution for a turn based mobile chess game? 0 Answers
How to smoothly interpolate game object positions in a network game? 0 Answers
How configure a dedicated server about Unet 0 Answers
Assign Network Manager an Online Scene with Script 0 Answers