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
1
Question by PandawanFr · Feb 22, 2017 at 08:32 PM · c#networkingmultiplayersynchronization

UNET Health isn't syncing

Hello,

I'm new to Unet and am struggling with syncing my health. For some reason, my health is only syncing and behaving correctly on the host. Here is a gif that shows my problems: http://i.giphy.com/26BGtudQCrjRlbf8Y.gif

Anyways, here is my code for the Player and the Player UI.

Thanks a lot!

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

Answer by MarshallN · Feb 22, 2017 at 09:49 PM

I've just been helping out another user with this ; take a look at the comment chain on this question.

Long story short, [SyncVar(hook)] only calls that hook on clients. If you observe the player health in the inspector, you should see it changing, but since the bar is only updated when the hook is called, the visuals are off. You should use [Command] and [ClientRpc] to sync a function between clients (example here), or else just have the health bar check in Update() if it's different than the health value itself, and if so change.

Comment
Add comment · Show 8 · 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 PandawanFr · Feb 22, 2017 at 10:06 PM 0
Share

Yeah but the thing is, I'm updating the ui with Update() inside the PlayerUI script, for now OnHealthChanged is only used to test and call Die()

avatar image MarshallN PandawanFr · Feb 22, 2017 at 10:14 PM 0
Share

Hmm... Looking at your code, you're only running Update if you're the local player, and if you are, then TakeDamage is run upon pressing $$anonymous$$. However, TakeDamage returns if you're not the host - meaning that any player who isn't the host can't take damage. I haven't worked with [ReadOnly] and can't find the reference for it, so I dunno if that's tripping you up, but that's what I see first.

avatar image PandawanFr · Feb 22, 2017 at 10:16 PM 0
Share

ReadOnly is just a custom script that I got online to allow me to see a variable in the inspector without allowing to edit it (just so I don't edit the wrong variables)

avatar image MarshallN PandawanFr · Feb 22, 2017 at 10:21 PM 0
Share

That'd be why I can't find its reference, then, haha. So that's not the issue.

If changing the TakeDamage function so that it doesn't return if it's not the server's script doesn't work, try adding a script that damages all players if a button is pressed, and test that on the host to see if it syncs properly.

avatar image PandawanFr MarshallN · Feb 23, 2017 at 12:40 AM 0
Share

Hey, so I rewrote my code to hopefully make it work better. So now, the health is getting synced up, but when I try to modify the UI from OnHealthChange, I get an error "RPC Function RpcSetHealthValue called on client."

Here's the code

Also, my RpcSetHealthValue is just this:

 [ClientRpc]
     public void RpcSetHealthValue(int value)
     {
        // healthBar is a UI slider object 
         healthBar.value = value;
     }

Show more comments
avatar image PandawanFr · Feb 23, 2017 at 01:22 AM 0
Share

Ok, so I got it to work! What I did is made all the methods that modify the health Commands, and completely removed the OnChangeHealth method as I found a way to work without it.

I also changed my update to this so that the TakeDamage wouldn't be called on all players when doing it on the client.

 private void Update()
     {
         if (isLocalPlayer)
         {
             if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$))
                 CmdAttack();
         }
     }
avatar image MarshallN PandawanFr · Feb 23, 2017 at 03:03 PM 0
Share

Nicely done, congrats on getting it to work!

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

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

SyncVar issue 0 Answers

Unity networking tutorial? 6 Answers

Synchronizing parented objects' position and rotation & handling player collision 0 Answers

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

Client can't spawn GameObject's 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