- Home /
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);
}
}
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();
}
how is in C#? what is "active"? I have SetActive, activeSelf, GetActive
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
I cannot thank you enough. Have been looking for this for my mmorpg for like 4 months lol. ty
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.
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.
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.
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
Follow this Question
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