- Home /
Unity PhotonNetwork globally enable a component
I'm currently developing a multiplayer FPS, and I faced a problem right now. I'm making a weapon switching system for my FPS game, but I have no idea how to do it.
The system I'm trying to make is pretty simple. Whenever I press a specified button, it should disable component, and enable other component. (It needs to be component not a gameobject) I used [PunRPC], tag but then I realised that it applies to every player which means everyplayer's gun will be enabled. So I'm asking to the internet on how to modify the component globally only for one player.
Answer by ChristianSimon · Mar 06, 2017 at 09:49 AM
Hi,
RPC is one option you have. Another is to use OnPhotonSerializeView(...) where you can synchronize boolean values in order to describe which component is currently active. This can look like this:
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
stream.SendNext(isComponentAActive);
stream.SendNext(isComponentBActive);
}
if (stream.isReading)
{
isComponentAActive = (bool) stream.ReceiveNext();
isComponentBActive = (bool) stream.ReceiveNext();
componentA.SetActive(isComponentAActive);
}
}
Make sure that, when you check your input, you only check the input of the local player to avoid changing the weapon for any other player. This should look like this:
public void Update()
{
if (!photonView.isMine)
{
return;
}
// Check input
}
Your answer
Follow this Question
Related Questions
Photon Cant see other players 1 Answer
Photon multiplayer, getting information from photonView.owner 1 Answer
[PHOTON PUN] Players cant shoot each other? 1 Answer
Photonview "this" does not exist in current context 1 Answer
When a host player changes his weapon all players get switched to it 1 Answer