- Home /
SyncList not initialized on server when created as localPlayerAuthority on client
I'm having a lot of trouble with SyncLists. I have a script on my player prefab that is being spawned by the default NetworkManager with localPlayerAuthority that needs to have a list of ints that are synced between clients.
Currently when I start a host and then connect as a client, the two player's SyncList's will be properly initialized on the client but on the server only the host's SyncList is initialized.
When I run the below code the console on the server reads:
NetID 3's sync list on editor contains 5 things
NetID 4's sync list on editor contains 0 things
While on the client it reads:
NetID 3's sync list on standalone contains 5 things
SyncList not initialized
SyncList not initialized
SyncList not initialized
SyncList not initialized
SyncList not initialized
NetID 4's sync list on standalone contains 5 things
Here is my code:
public class PlayingField : NetworkBehaviour
{
public SyncListInt monsterSyncList = new SyncListInt();
public GameObject tempMonsterCard;
void Awake()
{
monsterSyncList.Callback = OnMonsterChanged;
}
void Start()
{
if (hasAuthority)
{
Debug.Log("Is local player");
for (int i = 0; i < 5; i++)
{
monsterSyncList.Add(-1);
}
}
Debug.LogError("NetID " + netId + "'s sync list on " + (Application.isEditor ? "editor " : "standalone ") + "contains " + monsterSyncList.Count + " things");
}
public bool setMonsterCardByIndex(GameObject card, int i)
{
if (hasAuthority)
{
monsterSyncList[i] = card.GetComponent<ICard>().cardID;
return true;
}
return false;
}
private void OnMonsterChanged(SyncListInt.Operation op, int index)
{
if (SyncListInt.Operation.OP_SET == op && !hasAuthority)
{
if (monsterSyncList[index] != -1)
{
Debug.Log("Synced");
}
}
}
}
Any help would be appreciated. I've been working at this for hours with no end in sight.
Answer by Morgenstern_1 · Jun 23, 2016 at 08:51 AM
This is a really old topic I see, but what you're trying to do is sync data from a client to a server, which is not the purpose of sync lists regardless of who has authority.
You should ALWAYS add/remove from a SyncList on the server, and that data will get populated down to all the clients. SyncLists will not send data in the other direction.
It's generally bad practice to have anything other than the server changing data like that anyway - it opens up your game to cheating!
Your answer
Follow this Question
Related Questions
Inheritance and uninitialized synclists 2 Answers
ScriptableObjects and SyncVars, is it posible? 1 Answer
[UNet] Best practice to cache components in NetworkBehaviour 0 Answers
Unet CCU Exceeded by appID: Not going down 0 Answers
Changing rooms and spawning players with Multiplayer Example 2 Answers