Networking change of GameObject properties (color, size, etc)

Hello!

I’m working on and learning some basics of Unity 5, UNET, and networking. I made a simple 3D game where you go around and change the colors of objects. But I want to make it multiplayer now, and I am having lots of trouble figuring out how to send the changes over the network so all players can see a single player’s color change.

Part of the issue is that it has been difficult to find the answer using the newer UNET networking engine. And sometimes I come across answers that are for the older way.

So the main question is, how do I network non-player GameObject property changes? Color, shape, size, etc…

Here is some code I have now - and I’ve had many different versions so I’ll just post the current one:

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class Player_Paint : NetworkBehaviour {

	private int range = 200;
	[SerializeField] private Transform camTransform;
	private RaycastHit hit;
	[SyncVar] private Color objectColor;
	[SyncVar] private GameObject objIdentity;
	
	void Update () {
		CheckIfPainting();
	}

	void CheckIfPainting(){
		if(Input.GetMouseButtonDown(0)) {
			if (Physics.Raycast (camTransform.TransformPoint (0, 0, 0.5f), camTransform.forward, out hit, range)) {
				string objName = hit.transform.name;
			CmdPaint(objName);
			}
		}
	}

	[ClientRpc]
	void RpcPaint(){
		objIdentity.GetComponent<Renderer>().material.color = objectColor;
	}

	[Command]
	void CmdPaint(string name) {
	objIdentity = GameObject.Find (name);  //tell us what was hit
	objectColor = new Color(Random.value, Random.value, Random.value, Random.value);
	RpcPaint ();
	}
}

Edited: updated [ClientRpc] and [Command] functions. But still not working. It seems like the server is updated, but the client is not.

I figured it out:

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class Player_Paint : NetworkBehaviour {

	private int range = 200;
	[SerializeField] private Transform camTransform;
	private RaycastHit hit;
	[SyncVar] private Color objectColor;
	[SyncVar] private GameObject objectID;
	private NetworkIdentity objNetId;
	
	void Update () {
		if (isLocalPlayer) {	
			CheckIfPainting ();
		}
	}

	void CheckIfPainting(){
		if(isLocalPlayer && Input.GetMouseButtonDown(0)) {
			if (Physics.Raycast (camTransform.TransformPoint (0, 0, 0.5f), camTransform.forward, out hit, range)) {
				objectID = GameObject.Find (hit.transform.name);									// this gets the object that is hit
				objectColor = new Color(Random.value, Random.value, Random.value, Random.value);    // I select the color here before doing anything else
				CmdPaint(objectID, objectColor);
			}
		}
	}
	
	[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
	}
	
	[Command]
	void CmdPaint(GameObject obj, Color col) {
		objNetId = obj.GetComponent<NetworkIdentity> ();		// get the object's network ID
		objNetId.AssignClientAuthority (connectionToClient);	// assign authority to the player who is changing the color
		RpcPaint (obj, col);									// usse a Client RPC function to "paint" the object on all clients
		objNetId.RemoveClientAuthority (connectionToClient);	// remove the authority from the player who changed the color
	}
}

I had to use the “AssignClientAuthority” and “RemoveClientAuthority” and it works now!!!
Important things to note:
The objects which you wish to affect must have the NetworkIdentity component attached, and that must be set to LocalPlayerAuthority (If you forget, the warning errors are helpful to remind you).

This should work for any change you want to make on a non-player object that you want to sync with all clients. This example just changes the object to a random color.

Sir i have the same problem. CAn you please send me your project on dellqadir786@live.com