Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Pr0n · Jul 03, 2015 at 12:10 AM · networkingstate synchronization

SyncVar is not changing on server

Hello,

I'm trying to build game with new Unet. I have player with NIdentity, NTtransform etc.

alt text

In my script is var dirIndex for direction of player sprite (up, down, right, left). When is dirIndex changed it will be change sprite of SpriteRenderer. When is dirIndex changed on Host, changes will be conducted on Client, conversely, when Client change his dirIndex, server not register that.

 using UnityEngine;
 using UnityEngine.Networking;
 using System.Collections;
 
 public class Control : NetworkBehaviour {
 
     [SyncVar(hook="OnDirIndex")] 
     public int dirIndex;
 
     // Update is called once per frame
     void Update () {
 
         if (!isLocalPlayer) {
             Debug.Log(dirIndex.ToString());
             return;
         }
 
         position = rb.position;
 
         if (Input.GetKey(KeyCode.W)) {
             newPosition = position + Vector2.up * speed * Time.deltaTime;
             sr.sprite = playerSprites[0];
             dir = Direction.Up;
             dirIndex = 0;
         }
 
         if (Input.GetKey(KeyCode.S)) {
             newPosition = position - Vector2.up * speed * Time.deltaTime;
             sr.sprite = playerSprites[2];
             dir = Direction.Down;
             dirIndex = 2;
         }
 
         if (Input.GetKey(KeyCode.D)) {
             newPosition = position + Vector2.right * speed * Time.deltaTime;
             sr.sprite = playerSprites[1];
             dir = Direction.Right;
             dirIndex = 1;
         }
 
         if (Input.GetKey(KeyCode.A)) {
             newPosition = position - Vector2.right * speed * Time.deltaTime;
             sr.sprite = playerSprites[3];
             dir = Direction.Left;
             dirIndex = 3;
         }
 
         rb.MovePosition(newPosition);
         
         //handle shooting
         if (Input.GetKeyDown(KeyCode.Space)) {
             combat.CmdFire (GetDirection());
         }
 
         // center camera.. only if this is MY player!
         Vector3 pos = transform.position;
         pos.z = -50;
         Camera.main.transform.position = pos;
 
     }
 
     //will be called on clients when var changed on server
     private void OnDirIndex (int i) {
 
         dirIndex = i;
 
         switch (dirIndex) {
         case 0:
             sr.sprite = playerSprites[0];
             break;
         case 2:
             sr.sprite = playerSprites[2];
             break;
         case 1:
             sr.sprite = playerSprites[1];
             break;
         case 3:
             sr.sprite = playerSprites[3];
             break;
         default:
             break;
         }
     }
 
 }

Why client not changed hi dirIndex on server?

screen-shot-2015-07-02-at-23044-pm.png (33.0 kB)
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 n1gth · Jul 03, 2015 at 06:45 AM

The SyncVar will only work from server to client. It doesn't go the other way. For that, you could try using a Command function. See here.

Comment
Add comment · Show 4 · 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 SuckerForMayhem · Feb 28, 2017 at 07:06 PM 0
Share

So how exactly would that be done? I'm doing something fairly similar, trying to update a state of the player (crouching, sprinting, etc) based on booleans.

For above, trying to change dirIndex on the client, and then sending that to the server, so it gets updated on all the other clients; what would that look like?

Would it be something simply like:

 void Update() {
   if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W) {
     CmdSetDirIndex(0);
   }
 }
 
 [Command]
 void CmdSetDirIndex(int index) {
   dirIndex = index;
 }
 

If so man I lost a lot of time with messing with SyncVars... :)

avatar image SuckerForMayhem SuckerForMayhem · Feb 28, 2017 at 07:21 PM 0
Share

Hmm, that didn't seem to work. How can I use a Command function to update a state, and update all the client's states as well? I might spin up a new question.

avatar image Bratenhans SuckerForMayhem · Mar 01, 2017 at 07:56 PM 1
Share

Well, it should actually work with SyncVars and Command-Functions. Here is a example code with booleans:

 [SyncVar]
 bool var01 = false;   // will be synced on all clients
 [SyncVar (hook="OnHook")]
 bool var02  = false; // OnHook will be called on clients, when var02 gets altered on the server
 
 [Command]
 void Cmd_SetVar01(bool value){
      var01 = value;
 }
 
 [Command]
 void Cmd_SetVar02(bool value){
      var02 = value;
 }
 
 void OnHook(bool value){
      var02 = value;
      // other stuff
 }
 
Show more comments

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

6 People are following this question.

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

Related Questions

Sync Animations through Network (Mecanim) 0 Answers

HLAPI SyncVars property setter function 1 Answer

Unity networking tutorial? 6 Answers

HLAPI RPC synchronization - how to wait for a RPC 2 Answers

Synchronizing Variables across network 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