Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 darthbator · Jan 02, 2016 at 11:52 AM · networkingmultiplayernetworksynchronizationcommand

UNET : SyncVar value not updating, hooks firing

So I've run into a strange issue where SyncVar values are not updating on my clients, however their hook functions are being called!

In the game I am currently making I only track collisions and their related events on the server itself. So I have a damage Chain that Looks something like this.

First when a "bullet" object hits a player this code is run on the server instance of the bullet

 protected virtual void OnTriggerEnter2D (Collider2D collider) {
     if (!isServer || collider.gameObject == shooter.gameObject)
         return;
 
     Player playerHit = collider.GetComponent<Player>();
     if (playerHit) 
         playerHit.health.ServerRecieveDamage(damage);
 
     NetworkServer.Destroy(gameObject);
 }

Then damage is applied on the players health component. After damage is applied it fires off some client events based on the damage taken.

 [SyncVar(hook="CallbackTakeDamage")]public int health;
 
 [Server] public virtual void ServerRecieveDamage (int damageAmount) {
     health -= damageAmount;
     Debug.Log(health.ToString() + " is how much health we have!");
 }
 
 [ClientCallback] protected virtual void CallbackTakeDamage (int newHealth) {
     Debug.Log(health.ToString());
 
     if (newHealth <= 0) {
         if (DeathEvent != null) DeathEvent();
     } else {
         if (TakeDamageEvent != null) TakeDamageEvent();
     }
 }

Everything appears to get called fine. However the sync var doesn't appear to update it's value on any of the clients! However the callback function is invoked everywhere (they just stay at max health). I haven't run into anything like this before. Normally it's an all or nothing thing. I have never seen partial functionality from sync vars like this. Am I doing something wrong here?

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
1

Answer by Munchy2007 · Jan 02, 2016 at 12:27 PM

you need to do health = newHealth; in your hook function.

 [ClientCallback] protected virtual void CallbackTakeDamage (int newHealth) {
      health = newHealth;
      Debug.Log(health.ToString());
 10. 
      if (newHealth <= 0) {
          if (DeathEvent != null) DeathEvent();
      } else {
          if (TakeDamageEvent != null) TakeDamageEvent();
 15.     }
  }
 

When you use hook functions the syncvar isn't automatically updated, this is by design.

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.

Follow this Question

Answers Answers and Comments

56 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 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

Unity networking tutorial? 6 Answers

How to call [Command] on Client in UNet 1 Answer

Network.isServer / isClient not working 1 Answer

How fast do Syncvars synchronize? 1 Answer

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