- Home /
Question by
simbaorka101 · Aug 01, 2016 at 02:05 PM ·
unity 5networking
Using SyncListString/Synchronizing lists
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Networking;
using System.Linq;
public class Player : NetworkBehaviour {
[SyncVar(hook="OnPlayerIDChanged")]public string playerID;
private GameObject[] loadCards = new GameObject[39];
private Sprite[] loadSprites = new Sprite[39];
public SyncListString usedSprites = new SyncListString ();
private List<Sprite> sort = new List<Sprite> ();
private SpriteRenderer sRend;
private Transform tr;
private void GenerateCards ()
{
loadSprites = Resources.LoadAll<Sprite> ("Sprites/cards");
for (int i = 0; i < 10; i++) {
loadCards [i] = Instantiate (Resources.Load ("Prefabs/Card")) as GameObject;
//loadCards [i].transform.parent = GameObject.Find (string.Format("Player {0}", netId.Value)).transform;
while (sort.Count < 10) {
var index = Random.Range (0, 39);
if (!usedSprites.Contains (loadSprites [index].ToString ())) {
CmdAddUsedSprite (loadSprites [index]);
sort.Add (loadSprites [index]);
}
}
tr = loadCards [i].GetComponent<Transform> ();
tr.position = new Vector2 (0, -3.45f);
tr.RotateAround (new Vector3 (0f, -12.57f, 0f), Vector3.forward, -36 + i * 8);
}
sort = sort.OrderBy (p => p.ToString ().Substring (2, 3)).ToList ();
for (int i = 0; i < 10; i++) {
sRend = loadCards [i].GetComponent<SpriteRenderer> ();
sRend.sprite = sort [i];
}
sort.Clear ();
}
public override void OnStartLocalPlayer () {
CmdSetPlayerID (string.Format("Player {0}", netId.Value));
GenerateCards ();
}
public override void OnStartClient () {
OnPlayerIDChanged (playerID);
}
[Command]
void CmdAddUsedSprite (Sprite newSprite) {
usedSprites.Add (newSprite.ToString());
}
[Command]
void CmdSetPlayerID (string newID) {
playerID = newID;
}
void OnPlayerIDChanged (string newValue) {
playerID = newValue;
name = playerID;
if(isLocalPlayer) name += " Local";
}
}
This is the code i use to generate some playing cards in my multiplayer game, the generating of cards does work, the issue is the SyncListString usedSprites doesn't update across the server. I tried setting it up simmilar to playerID system which works fine but it doesn't seem to work. also I couldn't find any clear usage instructions online. Or if this is the incorrect way to transfer lists between clients across network could you point me in the right direction.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612085853im_/https://answers.unity.com/themes/thub/images/avi.jpg)