Change the player's color over the network

Hi, i am creating a game where the player can choose to be in the blue team or the red team, so I have a button “RedTeam” and a button “BlueTeam” who will affect the variable “myTeam” of the player who pressed the button, this is working but i can’t seem to figure out how to change the renderer of the player to assign the color of his team over the network, it only works locally. Here is the piece of code of my script:

public void SetColor()
	{
		
		if (isLocalPlayer == true) {
			CmdPlayerColors ();

		}
	}

[Command]
	void CmdPlayerColors()
	{
		if(myTeam == "Blue"){
			rend.material.SetColor("_Color", Color.blue);
			//Debug.Log (transform.name + " is blue");
		}
		if(myTeam == "Red"){
			rend.material.SetColor("_Color", Color.red);
			//Debug.Log(transform.name +" is red");
		}
	}

Thanks in advance.

2 Answers

2

@antiNT: You need RPC, here is how i do it. See if you can modify that.

CmdPaint (aGO, Color.yellow);

Executed this:

[Command]
	void CmdPaint(GameObject obj, Color col) {
		objNetId = obj.GetComponent<NetworkIdentity> ();        // get the object's network ID
		RpcPaint (obj, col);
	}

	[ClientRpc]
	void RpcPaint(GameObject obj, Color col){

		obj.GetComponent<Renderer>().material.color = col;      // this is the line that actually makes the change in color happen

	}

Why there is a "objNetId" ?

Ok, so I used your method and only the client can change it's color, the server will always be white (the default color)... Any ideas?

Anyone? I can’t continue the project without that…