How to know which object created in OnServerAddPlayer is for object of OnStartClient?

I am trying to link the audio source from a VoiceChat example to my playerPrefab.

But I can’t figure out how to do it?

When a network player joins, a “playerPrefab” is created in OnServerAddPlayer for all clients,
At the same time a voicechat “proxyPrefab” is created in OnStartClient for all clients.

How do I know which proxyPrefab was created for which playerPrefab?

My LobbyManager snippit:

public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId) {
    //Add the playerPrefab to the lobby scene and give it authority
    GameObject iObject = NetworkServer.FindLocalObject ( newLobbyGameObject.GetComponent<NetworkIdentity> ().netId );
    var playerPrefab = Instantiate(gamePlayerPrefab, Vector3.zero, Quaternion.identity);
    NetworkServer.SpawnWithClientAuthority (playerPrefab, iObject);
}

public override void OnStartClient(NetworkClient client) {
    VoiceChatNetworkProxy.OnManagerStartClient(client);
}


#region NetworkManager Hooks
public static void OnManagerStartClient(NetworkClient client, GameObject customPrefab = null)
{
    client.RegisterHandler(VoiceChatMsgType.Packet, OnClientPacketReceived);
    client.RegisterHandler(VoiceChatMsgType.SpawnProxy, OnProxySpawned);

    proxyPrefab = Resources.Load<GameObject>(ProxyPrefabPath);

    ClientScene.RegisterPrefab(proxyPrefab);
}

[UPDATE]
This is turning out to be the hardest thing I have ever had to do with Unity! A true nightmare and I mean it sucks!!

I fear I will get no help and will be fighting this for a very long time.

If I understood UNET better I might be able to figure it out, and it seems so simple, but Unity makes these things impossible and it’s super frustrating!!!.

I have now just wasted my entire night/morning on this and am more confused then when I started.

Is UNET incapable or is it me???

In cases like this I usually realized (after much hair-pulling) that I was looking at this issue slightly wrong. For us to understand what you issue is, you may want to try and lay out the issue you are having more clearly. I understand that you have an audio source and want to link it to your player prefab. This usually isn’t that difficult (the player prefab that represents “your” copy on the server has “isLocalPlayer” set active), so there is probably more to this problem, which you could perhaps explain so we all can take a shot at it.

Something else you may already be aware of: every networked object has a network ID component, and this component contains a networkID field. This netowrk id field is guaranteed to contain the same value across all clients. So the copy of our player object has the same netoworkID on all cleints, and you can use NetworkServer.findNetworkedObject or ClientScene.findNetworkedObject to find the exact object that is identified by your network ID, and make sure that you attach the audio to the correct object.

If this doesn’t help, just explain more of the issue you are running into, and eventually, we (the community) will figure it out with you. :slight_smile:

-ch