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
0
Question by Pearbook · Apr 17, 2016 at 12:05 PM · networkingnetworkdirectionspriterenderersync

Unet: Best way to Sync multiple variables

I'm pretty new to Unet and Networking. The biggest issue I'm currenlty running into is syncing my stuff to both Host(Server) and Client. Every time I think I know how it works, it just doesn't work like I expected.

I know I have to send something to the server with a [Command] and than sync it with a [SyncVar]. But this doesn't always work or is just to much if you have to sync multiple variables.

 private float InputAxis;
 
     [SyncVar (hook = "PlayerSpriteCallback")]
     public SpriteRenderer PlayerSprite;
     
     [SyncVar (hook = "ToolSpriteCallback")]
     public SpriteRenderer ToolSprite;
     
     [SyncVar (hook = "HandSpriteCallback")]
     public SpriteRenderer HandSprite;
 
     void Update () 
     {
         if(isLocalPlayer)
         {
             InputAxis = Input.GetAxis("Horizontal");
 
             if(InputAxis > 0)
             {
                 CmdDirectionChange(1);
             }
             else if(InputAxis < 0)
             {
                 CmdDirectionChange(-1);
             }
         }
     }
 
     [Command]
     void CmdDirectionChange(int direction)
     {
         if(direction == 1)                // Right
         {
             // un-flip the player sprite.
             PlayerSprite.flipX = false;
             
             HandSprite.sortingOrder = 2;    // Above Player, above tool.
             ToolSprite.sortingOrder = 1;     // Above Player, behind hand.
         }
         else if (direction == -1)         // Left
         {
             // Flip the player sprite.
             PlayerSprite.flipX = true;
             
             HandSprite.sortingOrder = -1;    // Behind Player above tool.
             ToolSprite.sortingOrder = -2;     // Behind Player and hand.
         }
     }
 
     void PlayerSpriteCallback(SpriteRenderer sprite)
     {
         PlayerSprite = sprite;
     }
     
     void ToolSpriteCallback(SpriteRenderer sprite)
     {
         ToolSprite = sprite;
     }
 
     void HandSpriteCallback(SpriteRenderer sprite)
     {
         HandSprite = sprite;
     }

This code only works on the Host (Server) and not on the Client player (I do have Local Player Authority selected). What am I doing wrong? Is it because I try to sync entire spriteRenderers? If so does that mean I have to sync every bool, int ect. in the direction change function? What if I have even more values I have to sync in the future, doesn't that end up in a gigantic mess eventually?

Comment
Add comment · Show 3
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 Guhanesh · Apr 18, 2016 at 06:11 AM 0
Share

I think SpriteRenderer data type can not be used as SyncVars. Only primitive data types like int,float,txt field can be used as shyncvars.

avatar image Pearbook Guhanesh · Apr 18, 2016 at 04:11 PM 0
Share

Hmm.. I guess that was expected. I just keep on thinking about how I could do this most efficiently (also for future usage). $$anonymous$$aking a syncvar for every value I want to sync sounds very messy and unefficient.

avatar image ThatBrianDude Pearbook · Apr 20, 2016 at 03:25 PM 0
Share

Just in case you don't know yet there is also SyncListint, SyncListbool etc. If you want to sync a list of custom objects you can use SyncListstruct. I am also currently trying to get this work.

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

SyncVar works with disabled components? 0 Answers

Unet Instantiate 0 Answers

UNET 5.3 Shooting Client/Server Sync issue 0 Answers

[UNET] TRANSFORMATION NOT SYNCING 0 Answers

Networking Sync SetActive Not Working 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