- Home /
PhotonNetwork RPC not working when disabling/enabling components
Hello, I'm currently developing a simple multiplayer fps game using PhotonNetwork, but i'm stuck at switching weapons.
I'm doing this by switching the MeshRenderer and MeshCollider on and off. If i'm the local player, I can see that my MeshRenderer and mesh Collider are turning off/on, although on the other client, I can't see if player has changed their weapon or not. My problem is that when I use PunRPC(PhotonNetwork RPC), it only changes for local player.
I'm currently spawning 2 weapons(1 for local player and 1 for other players). So code works like this: When player gets spawned, the object which isn't for the local player(which isn't going to display for local player but client) will get disabled for local player ONLY. Whenever local player presses a specified key, the weapon will switch by disabling the components.
Is there any condition when using RPCs?
If anyone is wondering, I only turned the script on for local player.
Here is my weapon switching code:
     void Update()
     {
 
         //Take input control to switch weapon
         if (Input.GetKeyDown(KeyCode.Alpha1) || Input.GetKeyDown(KeyCode.Alpha3))
         {
             //If knife animation is not going on
             if (Knifeanim.GetBool("IsSwinging") == false)
             {
                 //Switch weapon
                 GetComponent<PhotonView>().RPC("SwitchWeapon", PhotonTargets.All);
 
                 //Re-change layer of MYPLAYER's graphicsweaponholder. If this process is not going on,
                 //The player will be seeing 2 or more weapons at a time when switched
                 SetLayerRecursively(GraphicsWeaponHolder, 9);
             }
         }
     }
 
     //Initialize the weapon. Displays the weapon
     [PunRPC]
     public void InitializeWeapon()
     {
         //If the current Weapon is GUN:
         if (curWeapon == "isGun")
         {
             //Disable Knife
             GrKnife.GetComponent<MeshCollider>().enabled = false;
             GrKnife.GetComponent<MeshRenderer>().enabled = false;
 
             //Enable Gun
             GrPistol.gameObject.GetComponent<MeshRenderer>().enabled = true;
 
             //Disable knife for firstperson camera
             camknife.gameObject.SetActive(false);
             //Enable gun for firstperson camera
             campistol.gameObject.SetActive(true);
         }
         //If the current Weapon is KNIFE
         else if (curWeapon == "isKnife")
         {
             //Enable Knife
             GrKnife.GetComponent<MeshCollider>().enabled = true;
             GrKnife.GetComponent<MeshRenderer>().enabled = true;
 
             //Disable Gun
             GrPistol.gameObject.GetComponent<MeshRenderer>().enabled = false;
 
             //Enable knife for firstperson camera
             camknife.gameObject.SetActive(true);
             //Disable gun for firstperson camera
             campistol.gameObject.SetActive(false);
         }
     }
 
     //This function will be called when player pressed specified key
     [PunRPC]
     public void SwitchWeapon()
     {
 
         //If the key pressed is number "3":
         if (Input.GetKeyDown(KeyCode.Alpha3))
         {
             //Change current weapon to Knife
             curWeapon = "isKnife";
         }
         //If the key pressed is number "1":
         else if (Input.GetKeyDown(KeyCode.Alpha1))
         {
             //Change current weapon to Gun
             curWeapon = "isGun";
 
         }
 
         GetComponent<PhotonView>().RPC("InitializeWeapon", PhotonTargets.All);
     }
Your answer
 
 
             Follow this Question
Related Questions
error CS0117: `Util' does not contain a definition for `SetLayerRecursively 0 Answers
Multiplayer ball not syncing properly using mirror. 0 Answers
Do i need to turn on unity multiplayer in order to properly use Photon PUN 2? 0 Answers
When the game starts, how can I instantiate a ball onto a random client? 0 Answers
How to port forward via script in unity 0 Answers
