Network RPC

I made a script that changes the username, but I need to make it an RPC for all other players to see. Here’s what I have so far.

var userName : String = "Guest000";
var maxChar : int = 10;

var nameTag : TextMesh;

private var isDone : boolean = false;

function Update()
{
	nameTag.text = userName;
}

function OnGUI() 
{
	if (networkView.isMine)	
	{
		if(isDone == false)
		{
			userName = GUI.TextField(Rect(0, 0, 100, 25), userName, maxChar);
			
			if(GUI.Button(Rect(0, 25, 100, 25), "Done"))
			{
				isDone = true;
			}
		}
	}
}

You don’t make a script an RPC, you make a function an RPC. And it can’t be the OnGUI function.

No need to set nameTag.text every frame in Update (), only do that a single time when it changes.

if(GUI.Button(Rect(0, 25, 100, 25), "Done"))
         {
          isDone = true;
          networkView.RPC ("GetName", RPCMode.Others, userName);
         }
...
}

@RPC 
function GetName (name : String, info : NetworkMessageInfo) {

//do something with info.sender here...

}