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 Slences · Nov 10, 2015 at 06:56 PM · networkingsynchronizinghealth

Simple network script not working?

I have a very basic health SyncVar in my PlayerStats class:

 [SyncVar(hook = "OnHealthChange")]
 public float health;

 void OnHealthChange(float newHealth)
 {
     health = newHealth;
     if (newHealth > maxHealth)
         health = maxHealth;
     if (newHealth <= 0)
         health = 0;
  }

In the Player class, there is a serialized reference to this class. In this class I have a simple script to display the health bar:

 void OnGUI()
 {
     if (!isLocalPlayer)
         return;
 
     OfflineHealthBar.fillAmount = playerStats.health / playerStats.maxHealth;
     OfflineHealthText.text = playerStats.health.ToString() + "/" + playerStats.maxHealth.ToString();
  }

In another class, I have a server side method that increase the max health for the player, and automatically sets the current health to the max health:

   case "UpgradeHealth":
            playerStats.maxHealth += 50;
            playerStats.health = playerStats.maxHealth;
            break;

Everything works fine, however for some reason, when the players' max health is increased, the players' health won't be updated. E.g. when a players' max health increases from 1000 to 1050, the players' health will still display as 1000 unless this process is repeated, then it will display 1050 while the max health is 1100.

This problem only occurs on a client. When I debug my health in the PlayerStats class, it will debug the right health. However, when I debug it in the Player class, it will debug the wrong health.

What am I doing wrong here? Thanks in advance!

Comment
Add comment · Show 7
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 pekalicious · Nov 11, 2015 at 05:30 AM 0
Share

Should health = maxHealth; be health = newHealth;? Also, you are only syncing health, not maxHealth, is that correct?

avatar image Slences pekalicious · Nov 11, 2015 at 05:59 AM 0
Share

I don't think so. And yeah, maxHealth is also a syncvar.

avatar image pekalicious Slences · Nov 11, 2015 at 06:49 AM 0
Share

Yeah, my bad. Didn't notice you are already doing that. Hmmm..

avatar image Runalotski · Nov 11, 2015 at 12:26 PM 0
Share

Are you syncing the maxHealth before updating it so you say

Send max health over network

Increase maxhealth

when you would want it the other way around in that case.

avatar image Slences Runalotski · Nov 11, 2015 at 12:50 PM 0
Share

What exactly do you mean? Currently maxhealth will be increased, and then the health will be set to that amount.

avatar image seanr Slences · Nov 11, 2015 at 03:38 PM 0
Share

maxHealth is only being increased on the server

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by seanr · Nov 11, 2015 at 04:48 PM

The issue is that the hook for health is being called before the new value of maxHealth is applied on the client. This ordering is determined by the order the member variables are declared in the class.

If you switch the order that health and maxHealth are declared, then it works.

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 Slences · Nov 11, 2015 at 05:43 PM 0
Share

I never thought of that! Thank you very much.

avatar image
0

Answer by seanr · Nov 11, 2015 at 03:53 PM

you should post the entire script(s).

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 Slences · Nov 11, 2015 at 04:08 PM 0
Share

I $$anonymous$$ified the required classes and posted them here: http://hastebin.com/motanuxoza.avrasm. Thanks in advance!

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

41 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

Related Questions

Why the Network Health script is always null 0 Answers

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

Only host client is able to spawn objects 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