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 /
  • Help Room /
avatar image
0
Question by andysctu · Feb 15, 2016 at 10:07 AM · networkingnetworksetactiveactivesync

Syncing SetActive over network

I am trying to implement switching between two weapons. My method is currently:

1) Player prefab has both weapons as children. The first one is active while the second one is inactive to begin with.

2) When a player presses the switch weapon button ('R'), the second one becomes active and the first one become inactive (and vice versa).

When the host presses 'R', it calls RpcToggleWeapon() to toggle its weapon for all clients. When a client presses 'R', it calls CmdToggleWeapon() to make the server call RpcToggleWeapon(), to toggle its weapon for all clients (host included).

My problem:

If a client connects after a host/client already switched weapons, it will not register on the new client.

e.g.

Host starts game.

Client1 connects.

Client1 presses 'R'. Weapon is changed correctly on Host and Client1.

Client2 connects, but sees the wrong weapon for Client1.

Question 1)

Is my method of client2client communication correct? (call cmd to server then have it call rpc to all other clients) I feel there is a better way to sync SetActive...

Question 2)

If my method is the best way, how can I fix the issue described above?

I've attached some code for reference,

 void Update () {
     if (!isLocalPlayer) return;
     if (Input.GetKeyDown(KeyCode.R)){
         if (!isServer) { // Client
             CmdToggleWeapon();
         } else { // Host
             RpcToggleWeapon();
         }
     }
 }

 [Command]
 void CmdToggleWeapon(){
     RpcToggleWeapon();
 }

 [ClientRpc]
 void RpcToggleWeapon(){
     for (int i = 0; i < weapons.Length; i++) {
         weapons[i].gameObject.SetActive(!weapons[i].gameObject.activeSelf);
     }
 }

Thanks!

Comment
Add comment · Show 1
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 Salmjak · Feb 15, 2016 at 10:45 AM 0
Share

I'm not sure about this. But is the [Command] to [ClientRpc]chaining really necessary? As I understand it [Command] sends a funtion to be run at the server, using the clients data. [ClientRpc] sens a function to be run at clients, using the server data. So what you tell the game is: Tell the server to run this function on all clients, using the server data. So it's weird that it works at all, since it should be using the weapons[] that is on the server and not on the client. Am I wrong in this? Oh, and btw, you should probably try to sync the weapon change and have a separate "weapon update"-script. If you [SyncVar] GameObject weapon; Then new clients should recieve the update automatically. After that you just have to update the weapons locally.

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

[UNET] Transform sycing problem,[UNET] TRANSFORMATION SYCING PROBLEMS 0 Answers

Syncing hinge joint over network 0 Answers

syncvar not changing after command 0 Answers

Sync complex variables and components over network 1 Answer

Unity Mirror OnStartServer spawn GameObjects 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