- Home /
Multiplayer Problem: How to Change child object color on all instances of player?
I'm making a multiplayer PvP game where the players choose one of four factions, each with a color (red, blue, green, and yellow), and I have armor and weapons that should have their faction's colors applied to the highlight material of their respective items.
For instance, when Player A chooses the Red faction, their helmet, shield and sword highlight materials should change to red, not only on the local client, but on all clients' Player A's instances. I am new and confused by the Unity multiplayer code right now, and am trying hard not to give up. I have searched the sites for days for answers but I can't get anything to work properly.
Right now, to test, I have two cubes added to the test scene with code in the collider trigger to call the rpc method on the client's player object to change their color, but it only changes on the client's view, not updated and synced on all clients.
Cube code: using UnityEngine; using UnityEngine.Networking;
public class FactionChoice : NetworkBehaviour
{
public Faction FactionChange;
void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{
other.gameObject.GetComponent<PlayerNetworkControl>().CmdChangeFaction(other.gameObject, FactionChange);
}
}
}
PlayerNetworkControl code: using UnityEngine; using UnityEngine.Networking;
public class PlayerNetworkControl : NetworkBehaviour
{
//make public variables to reference scripts to turn on
public GameObject PlayerCamera;
public UnityStandardAssets.Characters.ThirdPerson.ThirdPersonUserControl Control;
public UnityStandardAssets.Characters.ThirdPerson.ThirdPersonCharacter Character;
private Material[] HelmetMaterials;
//Worn and equipped items
public GameObject Helmet;
[SyncVar]
private GameObject MyID;
[SyncVar]
public Faction MyFaction;
[SyncVar]
private Color FactionColor;
private NetworkIdentity NetworkID;
public override void OnStartLocalPlayer()
{
//turn on scripts here
PlayerCamera.GetComponent<Camera>().enabled = true;
Control.enabled = true;
Character.enabled = true;
GetComponent<PlayerActions>().enabled = true;
PlayerCamera.GetComponent<AudioListener>().enabled = true;
//if fighter, right now, everyone is a fighter!
Helmet.SetActive(true);
//attempt to change the highlight material color
HelmetMaterials = Helmet.gameObject.GetComponent<Renderer>().materials;
//set name here for debugging
gameObject.name = "Local Player";
base.OnStartLocalPlayer();
}
[Command]
public void CmdChangeFaction(GameObject PlayerAffected, Faction ChangedTo)
{
NetworkID = PlayerAffected.GetComponent<NetworkIdentity>();
NetworkID.AssignClientAuthority(connectionToClient);
RpcChangeMyFaction(PlayerAffected, ChangedTo);
NetworkID.RemoveClientAuthority(connectionToClient);
}
[ClientRpc]
public void RpcChangeMyFaction(GameObject Player, Faction NewFaction)
{
MyFaction = NewFaction;
switch(NewFaction)
{
case Faction.Blue: FactionColor = Color.blue; break;
case Faction.Green: FactionColor = Color.green; break;
case Faction.Red: FactionColor = Color.red; break;
case Faction.Yellow: FactionColor = Color.yellow; break;
}
Player.transform.FindChild("helmet").gameObject.GetComponent<Renderer>().material.color = FactionColor;
}
}
public enum Faction
{
Red,
Yellow,
Green,
Blue
}
Any help would be greatly appreciated.
Never$$anonymous$$d, I give up. The networking in Unity is too complicated for me to understand and there aren't enough resources or tutorials for this to be a feasible project for one person.
Nah dont give up, you look so close.
I've been playing in j$$anonymous$$onkeyEngine, and I GET the networking there. Perhaps I should delve deeper into the API or write my own. Honestly, I had to dump Windows and the Unity Linux Editor works only half the time I install it.