RPC method not getting executed

Hi folks!

Here is my problem: I got a chat network that works fine (all objects and scripts are already on scene). However, I cannot sync player positions (spheres, for the time being).

I spawn players like in the following snipet. There is an object in the scene that has the script

	private void OnConnectedToServer ()
	{
		NetworkViewID viewID = Network.AllocateViewID ();
		
		networkView.RPC ("SpawnNewPlayer", RPCMode.AllBuffered, viewID);
		
	}
	
	[RPC]
	void SpawnNewPlayer (NetworkViewID viewID)
	{
		
		GameObject player = Instantiate (spherePrefab, transform.position, transform.rotation) as GameObject;
		
		if (viewID.isMine) {
			player.GetComponent<PlayerController> ().Mine = true;
			
		} else {
			Destroy (player.transform.Find ("Sphere").transform.Find ("Main Camera").gameObject);
		
			player.GetComponent<PlayerController> ().Mine = false;
		}
		
		player.GetComponent<NetworkView> ().viewID = viewID;
	}

The spherePrefab has a NetworkView with State Sync off, and Observed set to None.
It also has a script with the following code

if (Mine) {
			if (Input.GetMouseButton (0)) {
				RaycastHit hit;
				
				if (Physics.Raycast (cam.ScreenPointToRay (Input.mousePosition), out hit, 100.0F)) {
					_moveTo = hit.point;
				
				}
			}
			
			Vector3 direction = _moveTo - _sphere.transform.position;
			
			direction = direction.normalized;
		
			Vector3 nextMovementStep = transform.position;
			nextMovementStep.x += direction.x * Time.deltaTime * speed; 
			nextMovementStep.z += direction.z * Time.deltaTime * speed;
		
			transform.position = nextMovementStep;
                        // this method never runs
			networkView.RPC ("SetPosition", RPCMode.Others, transform.position);
                   } 
	}

	[RPC]
	public void SetPosition (Vector3 newPosition)
	{
		Debug.Log ("im in");
		
		transform.position = newPosition;
	}

However, the SetPosition method is never executed. I am testing this while playing in two instances of the webplayer, in my browser.
The chat I implemented works like a charm, but this doesn’t. I have read the documentation and searched online and cannot, for the life of me, figure out why it won’t work. Each player can only control his own sphere. Other players are spawned, but just stand still. Also, there are no errors in the console.

Thanks in advance

Anyone?

This is driving me crazy!

I can’t see anything wrong with the code but can you explain why your generating your view id’s yourself ?

Because I am changing stuff on the players that I am spawning in the PlayerSpawner.

Anyway, I have been turning my head on this for 3 days now, makes no sense!

appels, I see now that I might not have answered your question. I generated my view id’s myself so that I can sync each instance of a player.
So say:
1 - player1 logs in, allocates a view ID for himself, and buffers an RPC
2 - player2 logs in, allocates a view ID for himself, and buffers an RPC
3 - player2 receives the RPC from player1, which contains the instantiate and view ID, so player2 spawn an instante of player1, and sets it view ID to be synced with player1
4- player1 receives the RPC from player 2, (…)

I think my rationality is right here, but it just does’t work.
I tried it now with Network.Instantiate. Same result ._.

Right, your trying to achieve what Network.instantiate does out of the box.

GameObject blah = (GameObject)Network.Instantiate(prefab, ....);

As soon as this gets called on one of the clients, the prefab should appear in all scenes connected to that server instance.
If this doesn’t work, you need to debug why it doesn’t work.
Turn on network debugging, then at least you can see if the buffered RPC’s reach the server and clients.
Don’t complicate your code unless you need to.

I was doing this as a tutorial, because I will be needing to sync events, not positions.
And right now I am not able to do it.
I debugged, the chat messages work fine (the object is already in the scene).
However, as far as the server is aware, the RPC’s for the SetPosition never get called. And the line that calls it gets executed… this is mind-boggling.

I managed to sync the players positions by setting the state sync to deliable, and as observed i put the script. I update the position with:

void OnSerializeNetworkView (BitStream stream, NetworkMessageInfo info)
	{
		if (stream.isWriting) {
			_pos = transform.position;
			stream.Serialize (ref _pos);
		} else {
			stream.Serialize (ref _pos);
			transform.position = _pos;
		}
	}

However, it lags like hell (hiccups on the movement).

Though, that is not the point. I want to be able to send events with RPC, and that isn’t working! :shock:

I figured it out.

I need to set the Observed property of NetworkView in the prefab to the script that will be handling the RPCs.
It is weird that I have to do it on instantiated objects, but not on objects that are already in the scene…

Anyways, thank you appels, you helped me to retain my sanity :wink:

EDIT:
If I instantiate the stuff manually it still doesn’t work ._.

Ok now I really got it.
The problem was that I didn’t have the RPC implemented in the server.

Can’t I just use the server to relay RPCs? Because Putting RPCs methods in the server that do nothing just so it works is tedious and ( to my eyes) completely unnecessary.

No, they have to exist in all instances.