Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Aces_Charles · Jul 16, 2015 at 08:23 AM · networkingrpccommand

UNet SyncVar not working

Hello,

I have spent the last couple of weeks learning about the UNet API and have all my movement scripts working perfectly, but I am having a problem with getting my item script working properly.

The way I thought I needed it to work is as follows:

  • Object triggers collider TriggerEvent

  • TriggerEvent sends a command to the server

  • Server calls a RPC on all clients which changes the value of a synced variable.

The active boolean is does not change though.

 public class Item : NetworkBehaviour {
     
     [SyncVar]
     public bool active = true;
     
     private void OnTriggerEnter(Collider other)
     {
         if(other.tag == "Player")
         {
             PickupItem(other.gameObject);
         }
     }
     
     public virtual void PickupItem(GameObject player)
     {
         CmdDisable ();
     }
 
     [Command]
     private void CmdDisable()
     {
         active = false;
         RpcDisable ();
     }
 
     [ClientRpc]
     private void RpcDisable()
     {
         active = false;
     }
 }


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

3 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by n1gth · Jul 16, 2015 at 08:40 AM

You don't need to change the variable on the client. The SyncVar changes automatically on the client when it is changed on the server. Your setup looks correct for this to happen - you perhaps should add Debug calls to ensure that the collider is working correctly.

If you want, you could add a hook on the client.

 [SyncVar(hook = "OnActiveChange")]
 private bool active = false;

Then when the value changes on the server, the hook is called on the client (which then takes responsibility for setting the value on the client).

 private void OnActiveChange(bool updatedActive)
 {
   active = updatedActive;
   Debug.Log("Active changed on client: "+active);
 }
Comment
Add comment · Show 6 · 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 Aces_Charles · Jul 16, 2015 at 03:00 PM 1
Share

Thanks for point out my mistake. I modified my code to fit what you suggested but OnActiveChange is not being called. The trigger is working.

 public class Item : NetworkBehaviour {
 
     [SyncVar(hook="OnActiveChange")]
     public bool active = true;
 
     private void OnTriggerEnter(Collider other)
     {
         if(other.tag == "Player")
         {
             print("Triggered");
             PickupItem(other.gameObject);
         }
     }
     
     public virtual void PickupItem(GameObject player)
     {
         CmdDisable ();
     }
 
     [Command]
     private void CmdDisable()
     {
         active = false;
     }
 
     private void OnActiveChange(bool updatedActive)
     {
         active = updatedActive;
         Debug.Log ("Active changed on client!");
     }
 }
avatar image n1gth · Jul 16, 2015 at 03:08 PM 0
Share

What you have looks correct. Are you on the latest patch version of unity? Have you started the game through the Network$$anonymous$$anager?

avatar image Aces_Charles · Jul 16, 2015 at 03:43 PM 1
Share

I am using 5.1.0f3. I am starting the game using the Network$$anonymous$$anager HUD component.

avatar image n1gth · Jul 16, 2015 at 04:17 PM 0
Share

You could try using the latest patch, I know several syncvar issues were fixed. It's 5.1.1p4.

avatar image Aces_Charles · Jul 17, 2015 at 05:39 AM 0
Share

I updated to the latest patch but that did not solve the problem. I also added a check to see if it was the local player because of a warning which was generated.

 public virtual void PickupItem(GameObject player)
     {
         if(isLocalPlayer)
             CmdDisable ();
     }

Also here is the class which is extending the base Item class.

 public class ItemHealth : Item {
 
     [SerializeField] float healAmount = 25f;
 
     public override void PickupItem (GameObject player)
     {
         //player.GetComponent<Player_Health> ().Heal (healAmount);
         base.PickupItem (player);
     }
 }
Show more comments
avatar image
3

Answer by Clemens-Scharfen · Jul 27, 2015 at 11:05 AM

Is it possible that you are trying to change your syncVar in the editor / inspector? As far as I have figured it out, you cannot make your syncVar changes in the inspector. At least for me, it only worked when I changed a synced variable by script.

 [SyncVar]
 private bool active = true;
 
 [Server] // Might also happen in a Command
 private void OnlyCalledOnServer()
 {
     // Change the syncVar by script, should affect server and client(s):
     active = false;
 }

I think, it works for n1gth because he uses a private variable and changes it in his code. So his answer is pretty fine, I guess. (By the way, the UNET Manual uses only private syncVars in its examples too.)

I am not sure if it is a bug or not, that it is not possible to change public syncVars in the editor. At least it is a little bit annoying because you cannot test it by changing the variable in the inspector, also not if you are using a private variable with the [SerializeField] Attribute additionally to the [SyncVar] Attribute. That is my experience with Unity 5.1.2 so far.

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 seanr · Jul 27, 2015 at 06:47 PM 1
Share

yes, the editor bypasses the accessor properties used by SyncVars when values are changed in the inspector, so SyncVar dirty flags are not set when changes are made in the editor :(

avatar image
1

Answer by chanyow · Feb 28, 2017 at 01:57 PM

you need to add your script on the player which has the networkIdentity.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

UNET OnTriggerEnter called due to placement of Network-GameObjects on Clients 0 Answers

Re-send ClientRPC after new player joins 1 Answer

Networking player input, UI, and ability use 1 Answer

Why is this not working properly? ClientRPC 0 Answers

Unity networking Command-function not being called 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