Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
2
Question by Slences · Sep 19, 2015 at 08:01 PM · networkinglistssynchronizing

SyncList not initialized?

I can't find a lot of documentation about the SyncLists variables, so I'm kind of stuck trying to use lists with gameplay important values. When I try to add values to a SyncList on a client, I always get the error: "SyncList not initialized". If I want to SyncVar one single variable I would do it simply like this:

 [SyncVar(hook="OnCooldown")]
 private float someCooldown;
 
 public override void OnStartLocalPlayer()
 {
     CmdServerSetCooldown();
 }
 
 [Command]
 public void CmdServerSetCooldown()
 {
      RpcInitializeCooldown();
 }
 
 [ClientRpc]
 RpcInitializeCooldown();
 {
     if(!isLocalPlayer)
            return;
 
     someCooldown = 5;
 }
 
 void OnCooldown(float newCooldown)
 {
      someCooldown = newCooldown;
 }

But let's say I have a list of all the players' cooldowns, how would I achieve the same thing? Given for example: "When the player connects, add 5 values to the Cooldowns list (e.g. all zero's) from the server and tell the client about it". Thanks in advance!

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
5
Best Answer

Answer by Baxter900 · Sep 20, 2015 at 10:36 PM

So first of all, it's worth pointing out that your current example is really weird, and shouldn't even work. So I'm going to start off by telling you a little about some of the netcode before moving on. First of all, you can only change SyncVar's and SyncLists from the server, never from the client. This is probably where your problem is actually, here's an outline of how your code currently runs.

 [SyncVar(hook="OnCooldown")]  // When someCooldown changes, run OnCooldown
  private float someCooldown;

Now it's worth noting real quick that the code your have in OnCooldown is functionally identical to just typing: [SyncVar] private float someCooldown; And just plain ignoring the hook.

  public override void OnStartLocalPlayer()  // When local player starts
  {
      CmdServerSetCooldown();  // Tell the server to set the cooldown
  }
 
  [Command]
  public void CmdServerSetCooldown()  // When the server is told to set the cooldown
  {
       RpcInitializeCooldown();  // Tell the clients to set the cooldown instead
  }

  [ClientRpc]
  RpcInitializeCooldown();  //When the client is told to set the cooldown
  {
      if(!isLocalPlayer)    // Check if you're the local player
         return;
 
      someCooldown = 5;  //This won't actually work...
  }

What you most likely want is this:

 [SyncVar]  // Make someCooldown sync
 private float someCooldown;
 
  public override void OnStartLocalPlayer()  // When local player starts
  {
      CmdServerSetCooldown();  // Tell the server to set the cooldown
  }
 
  [Command]
  public void CmdServerSetCooldown()  // When the server is told to set the cooldown
  {
       someCooldown = 5;  // set someCooldown to 5, since this is a syncvar, it will be sent to all clients
  }

Now if you want this to use SyncLists, it's not that much more difficult.

 [SyncVar]  // Make someCooldown sync
 private SyncListFloat someCooldowns = new SyncListFloat();
 
  public override void OnStartLocalPlayer()  // When local player starts
  {
      CmdServerSetCooldown();  // Tell the server to set the cooldown
  }
 
  [Command]
  public void CmdServerSetCooldown()  // When the server is told to set the cooldown
  {
      for(var i : int = 5; i>0; i--){  // Do this five times
       someCooldowns.Add(0);
   }
  }

If you want the equivalent of a hook on SyncLists, just add this code: void Awake() { someCooldowns.Callback = OnSomeCooldownsUpdated } private void OnSomeCooldownsUpdated(SyncListFloat.Operation op, int index) { // do something here }

Anyway, hope that helps!

Comment
Add comment · Show 1 · 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
avatar image asperatology · Feb 18, 2016 at 06:45 AM 0
Share

Your answer tremendously helped me erase some confusions with SyncLists.

I was wondering why SyncLists aren't being initialized and was about to suffer major setback, before co$$anonymous$$g to this question. I really loved how you provided examples explaining the premises. $$anonymous$$eep it up.

Also, you may want to edit the code at the bottom of your answer. It's missing some formatting.

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.

Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UNET OnTriggerEnter called due to placement of Network-GameObjects on Clients 0 Answers

How to find when the network transform component stops receiving update in UNET ? 0 Answers

Sync Soundtrack over Network 0 Answers

UNET Networking, syncronizing lists question 1 Answer

Proper way to send variables over the network 0 Answers


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