• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Darrek13 · Apr 02, 2017 at 10:03 AM · unity 5networkingsynchronizationsync

UNET SyncVar on a NetworkInstanceId SyncList structure not updating across network

Hi,

I've been looking for a solution on the web for the past hours, time to ask it myself... I have a SyncList structure with NetworkInstanceIds in it to share across all clients but it doesn't get updated. The list is up to date on the server for each player when I modify it, but it doesn't spread. I'm doing all the changes on the server and I don't know what to do to solve this.

Here's when I define the SyncVar :

     [SyncVar]
     public Structures.SyncListNetworkInstanceId secrets = new Structures.SyncListNetworkInstanceId();

Then I modify it here on the server only (I guess you don't need the full details of the project but I just need to pass the id of the secret objects) :

     [Server]
     public void DispatchSecrets()
     {
         // TODO : Something more proper and modulable to the number of players (have a problem with syncvar first to resolve)
 
         int i = 0;
         secrets[0].GetComponent<Secret>().shared = true;
         secrets[1].GetComponent<Secret>().shared = true;
         secrets[6].GetComponent<Secret>().shared = true;
         List<GameObject> tmpSecrets = secrets;
         tmpSecrets.Insert(5, secrets[0]);
         tmpSecrets.Insert(11, secrets[1]);
         tmpSecrets.Insert(10, secrets[6]);
         foreach (Player p in team1.GetComponent<Team>().players)
         {
             Debug.Log(p.deviceName);
             Debug.Log(tmpSecrets[i].GetComponent<Secret>().netId);
             p.secrets.Add(tmpSecrets[i].GetComponent<Secret>().netId);
             p.secrets.Add(tmpSecrets[i + 1].GetComponent<Secret>().netId);
             p.secrets.Add(tmpSecrets[i + 2].GetComponent<Secret>().netId);
             p.secrets.Add(tmpSecrets[i + 3].GetComponent<Secret>().netId);
             p.secrets.Add(tmpSecrets[i + 4].GetComponent<Secret>().netId);
             p.secrets.Shuffle();
             p.RpcUpdateProfile();
             i++;
         }
 
         i = 15;
         secrets[15].GetComponent<Secret>().shared = true;
         secrets[16].GetComponent<Secret>().shared = true;
         secrets[21].GetComponent<Secret>().shared = true;
         tmpSecrets.Insert(20, secrets[0]);
         tmpSecrets.Insert(26, secrets[1]);
         tmpSecrets.Insert(25, secrets[6]);
         foreach (Player p in team2.GetComponent<Team>().players)
         {
             Debug.Log(p.deviceName);
             Debug.Log(tmpSecrets[i].GetComponent<Secret>().netId);
             p.secrets.Add(tmpSecrets[i].GetComponent<Secret>().netId);
             p.secrets.Add(tmpSecrets[i + 1].GetComponent<Secret>().netId);
             p.secrets.Add(tmpSecrets[i + 2].GetComponent<Secret>().netId);
             p.secrets.Add(tmpSecrets[i + 3].GetComponent<Secret>().netId);
             p.secrets.Add(tmpSecrets[i + 4].GetComponent<Secret>().netId);
             p.secrets.Shuffle();
             p.RpcUpdateProfile();
             i++;
         }
     }


Can anyone help me ? That'd be great ! Thanks !

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Darrek13 · Apr 06, 2017 at 10:04 PM

OK so I figured out what was the problem !

The definition of my structure was wrong to be used with UNET, that's why it wasn't synchronizing over the network. Below is the correct code, hoping it'd help anyone :

 public static class Structures : object {
 
     public class SyncListNetworkInstanceId : SyncListStruct<NetID> { }
 
     public struct NetID
     {
         public NetworkInstanceId netID;
     }
 }

I changed a bit the core of the function below, but you just need to look at how I add new items to my structure.

     [Server]
     public void DispatchSecrets()
     {
         List<GameObject> tmpListSecrets = secrets.Take(secrets.Count / NUMBER_TEAMS).ToList();
         foreach (GameObject team in teams)
         {
             Team t = team.GetComponent<Team>();
             int numberOfSharedSecrets = t.players.Count == 1 ? 1 : t.players.Count - 1; // DEBUG : 1 to test with one player only (host test)...
             for (int i = 0; i < t.players.Count; i++)
             {
                 Player p = t.players[i];
                 for (int j = 0; j < numberOfSharedSecrets; j++)
                 {
                     Player nextPlayer = t.players[(i + 1) % t.players.Count];
                     Secret secret = tmpListSecrets[0].GetComponent<Secret>();
                     secrets.Find(s => s.GetComponent<Secret>().Equals(secret)).GetComponent<Secret>().shared = true;
                     p.secrets.Add(new Structures.NetID() { netID = secret.netId });
                     if (!nextPlayer.playerName.Equals(p.playerName))
                     {
                         nextPlayer.secrets.Add(new Structures.NetID() { netID = secret.netId });
                     }
                     tmpListSecrets.Remove(secret.gameObject);
                 }
                 int tmp = 5 - p.secrets.Count;
                 for (int j = 0; j < tmp; j++)
                 {
                     Secret secret = tmpListSecrets[0].GetComponent<Secret>();
                     p.secrets.Add(new Structures.NetID() { netID = secret.netId });
                     tmpListSecrets.Remove(secret.gameObject);
                 }
                 p.secrets.Shuffle();
                 p.RpcUpdateProfile();
             }
         }
     }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Why Does Adding to My SyncListStruct Break SyncVar? 0 Answers

Unity Networking: Object only spawning on the Host client. 2 Answers

SyncListStruct Unity Error 0 Answers

Synchronizing Item Crates 0 Answers

How to sync non-player objects with uNet? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges