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 /
  • Help Room /
avatar image
0
Question by Jenmar75 · Sep 14, 2020 at 02:43 AM · networkingsynchronizationmirror

What is the best way to send data from a client to the server using Mirror?

Hi guys! I've been working at this for a few weeks now (I know) and I've finally given in to my struggles. I'm new to Mirror and Networking as a whole, but not Unity. if I'm making some stupid mistake please let me know! Ok, here's my issue. I have a Player Health script and I have a function "Damage" that is being called from my wep script. I know that the function is being called properly (I check like a million times).

Some errors I get: Trying to send command w/o auth. I do not know why this is happening. The error comes from the line " CmdSyncHP(HP);" in the Damage function. I do know that clients cannot call cmds on other clients.

Some weird effects: When the host shots a client dmg is done and shown, when the client shoots a host/client it does nothing.

Here's my script:

 using Mirror;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class PlayerHealth : NetworkBehaviour
 {
     [Header("Main")]
     [SyncVar]
     [SerializeField]int HP;
 
     [Client]
     public void Damage(int damage)
     {
         HP -= damage;
         CmdSyncHP(HP);
     }
 
     [Command]
     void CmdSyncHP(int Sync)
     {
         RpcSyncHP(Sync);
         Debug.Log("Cmd HP");
     }
 
     [ClientRpc]
     void RpcSyncHP(int Sync)
     {
         HP = Sync;
         Debug.Log("Rpc HP");
     }
 }

This is on my player root object. The Player Identity game object is set to Observers and has Client Auth. I really do feel like there is something very simple that I did wrong. If you need further info please let me know! I've tried a million different ways and things. I'm all out of ideas! Thanks for your help! One last thing, this is my first time posting 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 Loles · Dec 21, 2020 at 05:48 PM

Hello, @Jenmar75 [SyncVar] Can not be modified on Client. You should alter it on server, and Mirror will automatically synchronize it with all the clients. So something like this should work using Mirror; using UnityEngine; using UnityEngine.UI;

  public class PlayerHealth : NetworkBehaviour
  {
      [Header("Main")]
      [SyncVar]
      [SerializeField]int HP;
  
      [Client]
      public void Damage(int damage)
      {
          CmdSyncHP(damage);
      }
  
      [Command]
      void CmdSyncHP(int damage)
      {
          HP -= damage;
      }
  }


Or you can detect, when player should be damaged on server (not on client), just change that [SyncVar] HP there and don't do this communication at all. This should be also preffered as the player can lie to you.

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

260 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

Related Questions

Unity Mirror OnStartServer spawn GameObjects 1 Answer

How do i get the two client's units to match up rather than be incorrect units? 1 Answer

Setting function to run on all clients but only work on the player that called it 0 Answers

Unet NetworkServer.spawn, correcting my workflow: Found no behaviour for incoming Command 1 Answer

UNET Can't set up nicknames above players properly 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