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 /
avatar image
1
Question by chrisall76 · Mar 31, 2014 at 04:08 AM · fpsphotonfind

Photon FPS disable/enable objects over network?

Hi all, My player has multiple weapons, and it works by disabling/enabling weapons depending on what your using. Now it works locally (of course), but over network it doesn't. I hear that GameObject.Find doesn't find inactive objects, so I'm guessing PhotonView.Find() won't work on inactive objects. Seems to be the case, so I need help. How would I go about doing this? Here's the code for switching weapons

 using UnityEngine;
 using System.Collections;
 
 public class WeaponHandler : Photon.MonoBehaviour {
 
     private GameObject EquipWeapon;
     public GameObject[] Weapons;
 
     void Start () {
         EquipWeapon = Weapons[0];
         Weapons[0].SetActive(true);
     }
 
     void Update () {
         if(Input.GetKeyDown(KeyCode.Alpha1) && EquipWeapon != Weapons[0]){
             SwitchWeapon(EquipWeapon, Weapons[0]);
         }
         if(Input.GetKeyDown(KeyCode.Alpha2) && EquipWeapon != Weapons[1]){
             SwitchWeapon(EquipWeapon, Weapons[1]);
         }
         if(Input.GetKeyDown(KeyCode.Alpha3) && EquipWeapon != Weapons[2]){
             SwitchWeapon(EquipWeapon, Weapons[2]);
         }
     }
 
     void SwitchWeapon(GameObject old, GameObject NewW){
         if (photonView.isMine || PhotonNetwork.connected == false) {
             EquipWeapon = NewW;
             old.SetActive (false);
             NewW.SetActive (true);
         }
         if (PhotonNetwork.connected == true) {
             photonView.RPC ("Enable", PhotonTargets.AllBuffered, EquipWeapon.GetPhotonView().viewID,  NewW.GetPhotonView().viewID);
         }
     }
 
     [RPC]
     public void Enable(int old, int NewWW){
         PhotonView.Find (old).gameObject.SetActive (false);
         PhotonView.Find (NewWW).gameObject.SetActive (true);
     }
 }
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by NightShroud · Apr 22, 2014 at 01:06 PM

i hate being the necromancer but this problem has been really annoying me for a while, i finally just figured it out by myself but i came across this thread while looking for an answer, so ill put the answer here for anyone else looking for it, and this thread isnt too old so maybe the OP might still be searching for it too

so this is how i fixed it with JS (gun1 gun2 gun3 are the different guns which are gameobjects, using OP's case as an example), using onphotonserializeview:

 function OnPhotonSerializeView(stream : PhotonStream, info : PhotonMessageInfo)
 {
     if(stream.isWriting)
     {
         stream.SendNext(gun1.active);
         stream.SendNext(gun2.active);
         stream.SendNext(gun3.active);
 
         
     }
     
     
     else
     {
         gun1.active = stream.ReceiveNext();
         gun2.active = stream.ReceiveNext();
         gun3.active = stream.ReceiveNext();
 
         
         
         
     }
 
Comment
Add comment · Show 4 · 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 alarm656 · Mar 18, 2016 at 12:55 PM 0
Share

how is in C#? what is "active"? I have SetActive, activeSelf, GetActive

avatar image Arshd alarm656 · Jun 14, 2016 at 05:00 AM 1
Share

It will be like this:

 // previous code
         if (stream.isWriting)
         {
             stream.SendNext(gun1.activeSelf);
             stream.SendNext(gun2.activeSelf);
             stream.SendNext(gun3.activeSelf);
         }
         else
         {
             gun1.SetActive((bool) stream.ReceiveNext());
             gun2.SetActive((bool) stream.ReceiveNext());
             gun3.SetActive((bool) stream.ReceiveNext());
         }
 // next code



avatar image SPOTNINJADUD7890 Arshd · Jan 04, 2017 at 09:49 AM 0
Share

I cannot thank you enough. Have been looking for this for my mmorpg for like 4 months lol. ty

Show more comments
avatar image
0

Answer by Bovine · Mar 31, 2014 at 06:18 AM

When your game starts have all then weapons enabled. You could find them using GetComponentsInChildren() if they have a particular component on them, or tag them and use FindObjectsWithTag(), though that is more problematic as I think it finds everything. Having found them, save the objects in your script, then disable all the weapons and remember which you've left enabled and active.

I find the approach of having everything enabled so I can find it and then disabling it is the best approach and then you're caching things and not calling slow methods like find during the frame, albeit not very often.

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 chrisall76 · Mar 31, 2014 at 06:47 AM 0
Share

Thing is, I'm pretty sure that's how it's working atm. Current equipped weapon is in EquipWeapon, and all weapons the player has is in the Weapons list. Now I need to know how I would translate all that to what I need to do in Photon.

avatar image Bovine · Mar 31, 2014 at 07:05 AM 0
Share

No, in terms of finding the GameObjects concerned this is not what's happening.

Presumably PhotonView and WeaponHandler are not your classes?

PhotonView.Find() is doing your object finding but I don't see where this class is, so presumably PhotonView.Find() is a static method. I guess this could be doing the above, but I doubt it.

Ahh, I see you have your own (dubiously named) $$anonymous$$onoBehaviour base class hidden in the Photon namespace.

I suggest you post in your question an edit containing that class.

avatar image chrisall76 · Mar 31, 2014 at 07:33 AM 0
Share

PhotonView is part of Photon, what it basally does it find a gameObject based off it's viewID. WeaponHandler is my own created class. I'm finding it off the viewID as I don't think a RPC can take a gameObject itself.

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

27 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

Related Questions

Photonview "this" does not exist in current context 1 Answer

Network RPC find sender gameobject 2 Answers

How to hide an object from other clients using PUN 2 0 Answers

Photon Network MultiPlayer names Help C# 3 Answers

Problem with UFPS and photon 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