Question by
Anton404 · Jun 21, 2018 at 08:07 PM ·
scripting problemnetworkingfpsweapon
How to set gamobjects active on network?
I know this is a question asked a lot, but I can't seem to figure it out. I am making an FPS. I have my weapon switching script set up, but the problem is that when I try to switch weapons, it only switches for the client. I have been banging my head against the wall for the last week, but I just don't get it. here's the script:
using UnityEngine;
using UnityEngine.Networking;
public class WeaponSwitching : NetworkBehaviour {
[SyncVar]
public int selectedWeapon = 0;
void Start() {
RpcSelectWeapon();
}
// Update is called once per frame
void Update()
{
int previousSelectedWeapon = selectedWeapon;
if (Input.GetAxis("Mouse ScrollWheel") > 0f)
{
if (selectedWeapon >= transform.childCount - 1)
selectedWeapon = 0;
else
selectedWeapon++;
GetComponent<NetworkView>().RPC("SelectWeapon", RPCMode.All, selectedWeapon);
}
if (Input.GetAxis("Mouse ScrollWheel") < 0f)
{
if (selectedWeapon <= 0)
{
selectedWeapon = transform.childCount - 1;
}
else
selectedWeapon--;
GetComponent<NetworkView>().RPC("SelectWeapon", RPCMode.All, selectedWeapon);
}
if (previousSelectedWeapon != selectedWeapon)
{
RpcSelectWeapon();
}
}
[RPC]
void RpcSelectWeapon()
{
int i = 0;
foreach (Transform weapon in transform)
{
if (i == selectedWeapon)
{
weapon.gameObject.SetActive(true);
}
else
{
weapon.gameObject.SetActive(false);
}
i++;
}
}
}
Any help is Greatly Appriciated! Thanks in Advance.
Comment