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 JaredHD · Oct 01, 2017 at 07:04 AM · networkingmultiplayerrpcclientsync

Using .SetActive() over Unity Network for multiplayer. (Enable weapons on clients)

Hello, I've been struggling to get this right. I've googled around quite a bit and have found examples of other people using [ClientRpc] to activate objects over a network which I am trying to do but I think I am doing it wrong.

What I am trying to do it activate a weapon on my player and have all other clients see the weapon. All the weapons are attached to the player at once but are deactivated at start. When I want the weapon, I call SetActive() and activate the weapon I want.

Here is my full weaponManager Script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Networking;
 
 public class WeaponsManager : NetworkBehaviour
 {
     //The Gameobject holding the weapons
     [SerializeField]
     GameObject weaponHolder;
 
     //Holds all of the players weapons
     [SerializeField]
     GameObject[] Weapons;
     int weaponNumber = 0; //Used to scroll through weapons in inventory through Weapons Array
 
     //For now just increase weapons when "1" is pressed
     private void Update()
     {
         if (Input.GetKeyDown(KeyCode.Alpha1))
         {
             weaponNumber++;
             //Apply weapon through out the network
             CmdSwapWeapon();
         }
     }
 
     //Gives the player Their start Weapon
     private void Start()
     {
         weaponNumber = 0;
         //Apply weapon through out the network
         CmdSwapWeapon();
     }
 
     [Command]
     void CmdSwapWeapon()
     {
         //Apply it to all other clients
         RpcSwapWeapon();
     }
 
     [ClientRpc]
     void RpcSwapWeapon()
     {
         //Disable previous weapon
         if (weaponNumber > 0)
             Weapons[weaponNumber - 1].SetActive(false);
 
         //If weapon Number is greater than all weapons in inventory, set it to 0 for weapon 1
         if (weaponNumber >= Weapons.Length)
         {
             weaponNumber = 0;
         }
         //Set current weapon active
         Weapons[weaponNumber].SetActive(true);
     }
 }

If the above is too much, I can split up each method and explain it. Basically all the above script does is on start, it assigns the default weapon. Than when, "1" is pressed, it swaps to a new weapon and deactivates the last weapon

Here is an image of what the script looks like in the editoralt text

The above works on the local player but it does not sync. Please ask if you have any questions.

weaponmanager.png (11.7 kB)
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 FernandoGBR · Oct 01, 2017 at 08:13 AM

Assuming that every player has the authority on his playerPrefab. Assuming that you want a non server-authority model (own players decide themseleves if the weapon switch can be done).

Some things to considerate: - You can only send Rpc from the server instance. - You can only ask for Cmd from the instance that you have the authority.

With all the assumptions and considerations that is what i would do:

  1. Add the weapon manager script to the player prefab (if it is not done)

  2. on update, check if you are the local player (the script will be running for each player instance, and you only want to affect the player that has the authority in that machine). you can do if(isLocalPlayer) or if(hasAuthority). If you arent just return

  3. From update, call to localSwapWeapon. This function will do what currently is inside your RPC, also that currentWeapon++

  4. Also from update, do the following:

     if(isServer){
               RpcSwap();
         }else{
               CmdSwap();
         }
    
    

this will ask all the clients to swap if you are the server, or the sewrver to swap if you are a client

  • RpcSwap will only call localSwap . Making the clients that receive the rpc to update

  • CmdSwap will call localSwap and RpcSwap. Making the host update the changes and distribute them.

  • On top of RpcSwap ask if(isLocalPlayer) and return (because you are receiving back the notification that you started)

Comment
Add comment · Show 3 · 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 JaredHD · Oct 01, 2017 at 08:44 AM 0
Share

Thanks for your help. I've changed the code as followed and it now looks like this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Networking;
 
 public class Weapons$$anonymous$$anager : NetworkBehaviour
 {
     //The Gameobject holding the weapons
     [SerializeField]
     GameObject weaponHolder;
 
     //Holds all of the players weapons 
     [SerializeField]
     GameObject[] Weapons;
 
     int weaponNumber = 0; //Used to scroll through weapons in inventory
 
 
     //Gives the player Their start Weapon
     private void Start()
     {
         weaponNumber = 0;
 
         //Apply weapon through out the network
         localSwapWeapon();
         if (isServer)
         {
             RpcSwapWeapon();
         }
         else
         {
             CmdSwapWeapon();
         }
     }
 
     private void Update()
     {
         if (!isLocalPlayer)
             return;
 
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Alpha1))
         {
             localSwapWeapon();
             if (isServer)
             {
                 RpcSwapWeapon();
             }
             else
             {
                 CmdSwapWeapon();
             }
         }
  
     }
 
     void localSwapWeapon()
     {
         weaponNumber++;
         //Disable previous weapon
         if (weaponNumber > 0)
             Weapons[weaponNumber - 1].SetActive(false);
 
         //If we are greater than all weapons in inventory, set it to 0
         if (weaponNumber >= Weapons.Length)
         {
             weaponNumber = 0;
         }
         //Set current weapon active
         Weapons[weaponNumber].SetActive(true);
     }
 
     [Command]
     void CmdSwapWeapon()
     {
         //Apply it to all other clients
         localSwapWeapon();
         RpcSwapWeapon();
     }
 
     [ClientRpc]
     void RpcSwapWeapon()
     {
         if (isLocalPlayer)
             return;
         localSwapWeapon();
     }
 }

The same issue happens. Both players spawn in and only the local player can see the weapon.

Also on top of that I seem to get a warning in unity: Trying to send command for object without authority.

Both player prefabs in the editor have Local Player Authority selected in the editor.

avatar image FernandoGBR JaredHD · Oct 01, 2017 at 09:20 AM 0
Share

What do u mean with "Both player prefabs in the editor have Local Player Authority selected in the editor."

You are supposed to have only one player prefab, and set it into the player prefab slot in the network manager. This will make a new player spawn every time a new player joins the network. With the Local Player Authority checkbox marked in the prefab every client will be able to send commands only from their own playerObject.

For clarify how this works, lets talk about what must happen in a mor formal way.

When you launch the host instance lets call it A. a new player is created, the host player. Lets call it A0.

When you launch the client instance, lets call it B. 3 new players are created. A1, which is the client player in the host instance. B0 which is the host player in the client instance. And B1, which is the client player in the client instance. A0 and B1 have player authority for A and B respectively.

When yo do something in A instance, your are supposed to do it on A0. And as you are the host, you only need to ask B0( and other possible C0, D0, ... ) via RPC for do the same thing.

When you do something in B instance (C, D, ...), you are supposed to do it on B1 (C2, D3, ...), who has the authority. Then ask by Command to A1 (which is yourself in the server) to do the same thing and ask the rest (C1, D1, ...) to do the same thing too.

The Trying to send command for object without authority. warning is just because you are sending Rpc without the authority as i explain u above.

avatar image JaredHD FernandoGBR · Oct 01, 2017 at 09:39 AM 0
Share

Thank you. It was a stupid mistake on my end. I had attached the Gun Holder to the Camera. So when the Client joined, I disabled his camera, which in turn disabled the guns on the client. But you definitely helped me understand more about networking.

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

133 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

Related Questions

Unity networking tutorial? 6 Answers

Sending Animation Over Network 0 Answers

Requesting data from server 1 Answer

Multiplayer - Opposite names above players 1 Answer

Can a person be both a client and a server? (and more) 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