how to Stream.serialize(stringValue)

I know Stream.serialize does not sync string value…

actually, in my multiplayer, I want to get some variable from other’s player by pointing raycast to instantiated player(remote) , the problem is variable won’t syncronized between local player and instantiated remote player on other computer , and raycast getComponent returns null…

so if it impossible, please help me to figure this by RPC,in this situation (raycast selecting)?
any help is appreciated… :smiley:

edit:

OK, Sorry… my achievement is getting “status” directly from raycast

  1. As selector, I put this script on Camera:
function Update(){
	var ray : Ray = camera.main.ScreenPointToRay(Input.mousePosition);
	var hit : RaycastHit;
	if (Physics.Raycast (ray.origin, ray.direction, hit, 100)) {
		////////////reading status other player///////////////////
		if (hit.collider.tag=="player"){
		Debug.Log(hit.collider.gameObject.GetComponent("PlayerData").Status);
		}
	}
}

2.and in player prefab i added PlayerData.js and attach Network view observed to this :

var statusUpdated:boolean;
var Status:String; function

OnSerializeNetworkView(stream :BitStream, info : NetworkMessageInfo){
  if (statusUpdated==false){
     statusUpdated=true;
     stream.Serialize(this.status); 	
  }
}

and the problem still same,serializing string value on the last line.

Did you read http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnSerializeNetworkView.html ?

OnSerializeNetworkView should be made in two parts (reading and writing) and you cannot directly use a class variable but must use a local variable instead (it is more visible in C# where the ref keyword has to be used).

OnSerializeNetworkView(stream : BitStream, info : NetworkMessageInfo) {
    var _status : String;
    if (stream.isWriting) {
        _status = this.status;
        stream.Serialize(_status);
    }
    else
    {
        stream.Serialize(_status);
        this.status = _status; 
    }
}

Never use the string version of GetComponent. The compiler can’t determine the type of the returned component so it’s just a Component. Use

hit.collider.GetComponent(PlayerData).Status

Beside that have you checked if the string is synced in the inspector? Make sure you observe the script component and not the Transform component. You have to drag the script instance onto the observed variable.

edit

RPCs have to be send manually when you want to update a value. Don’t send it each frame or something like that. You should send it whenever you change the status. The best way is to use a function to set a new state.

var Status:String;

function SetState(newState : String)
{
    Status = newState;
    networkView.RPC("OnReceiveState", RPCMode.Others, Status);
}

@RPC
function OnReceiveState(newState : String)
{
    Status = newState;
}

Whenever you want to change the Status variable use SetState("my state"); instead of Status = "my state";