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 NanoEngine · Feb 19, 2017 at 10:48 PM · multiplayer-networkingweapon system

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);
     }

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

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

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


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