Network - Killing Player, Then Respawn - Issue with RPC


I’m starting to get to grips with Unity’s networking tools. Pretty awesome stuff… Anyway, I’m working on a kill script and a respawn script, both are very close to working!

If I am my network (so, I’m the dude getting killed):

My Kill script (attached to player) is basically watching out for my health, if it drops below 0, then initiate blood splatter and sends player.transform to spawn script (attached to GO on root).

My Spawn script then turns off the player.gameObject, waits a few seconds, changes the players position to a randomly selected spawn point, turns the gameObject back on, restores health.

My kill script also initiates an RPC, but then is when it goes a little weird… I keep changing my script around, but whats happening right now. I, the player spawns, in a randomly selected point… as do my clients, but they spawn in a different place according to their player. In other words, I die, I spawn top left of map, but the person who killed me (client) sees me spawn bottom right of map.

I think its to do with my randomly selecting spawn point (spawn = Random.Range(0, spawnPoints.length):wink:

I hope your still with me, here is my HealthSystem (Kill - attached to player):

function Update(){
// -- We are dead -- \\
	if(currentHealthPercent < 1 || currentHealth < 1){
		alive = false;

	if(alive == false){
			//kill us (turn off gameObject)
			//tell clients to turn off gameObject too
			var viewID = Network.AllocateViewID();
			networkView.RPC("SendKillPlayer", RPCMode.All, viewID);
			//alive = true;

function KillPlayer(player : Transform){
	print("You Died...");
	//blood splatter
	var randomRotate = Random.Range(0,360);
	var clone : Transform;
	clone = Network.Instantiate(bloodSplatter, Vector3(transform.position.x,0.01,transform.position.z), transform.rotation, 0);
	//randomly respawn player
	for (var go in FindObjectsOfType(GameObject)){
		go.SendMessage("RespawnPlayer", player, SendMessageOptions.DontRequireReceiver);
		print("Respawning Player...");

function SendKillPlayer(viewID : NetworkViewID){
	for (var go in FindObjectsOfType(GameObject)){
		go.SendMessage("RespawnPlayer_CLIENT", transform, SendMessageOptions.DontRequireReceiver);
		print("Respawning Player CLIENT...");

And here is my SpawnPrefab Script (Spawn - attached to root hierarchy)

var spawn : int;

function RespawnPlayer(character : Transform){
	spawn = Random.Range(0, spawnPoints.length);
	RespawnPlayer_SELF(character, spawn);
	//var viewID = Network.AllocateViewID();
	//networkView.RPC("RespawnPlayer_CLIENT", RPCMode.All, viewID, character);

function RespawnPlayer_SELF(player : Transform, spawn : int){
	print("** Respawn Active! **");
	//turn off player << LOCALLY
	//wait a few seconds
	yield WaitForSeconds(3);
	//randomly respawn player
	player.transform.position = spawnPoints[spawn].transform.position;
	print("Reinitiating Player...");
	//restore health
	health = player.gameObject.GetComponent(HealthSystem);
	health.currentHealth = health.maxHealth;
	//set health controller's alive back to true
	health.alive = true;

function RespawnPlayer_CLIENT(client : Transform){ //, spawn : int){  viewID : NetworkViewID

	print("******* RPC ACTIVE - CLIENT *******");
	//turn off player << CLIENT
	//wait a few seconds
	yield WaitForSeconds(3); //extra time (incase of lag)
	client.transform.position = spawnPoints[spawn].transform.position;
	//restore health < CLIENT (SHOULD BE EQUAL TO PLAYER)
	health = client.gameObject.GetComponent(HealthSystem);
	health.currentHealth = health.maxHealth;
	//turn players renderer on, but deactivate rendererd guns, AND direction arrows < CLIENT
	var weapons = client.gameObject.GetComponent(WeaponController);
	var character = client.gameObject.GetComponent(CharacterMove);

… Well thats a mouth full. I think, what I need to do, is have the Random value that selects my random spawn point, the same for local and clients, but I have no idea how to pass on those variables with the method I’m using!

I can’t help you too much, I can only share a part of code (mainly, the original source is TPS Unity3D tutorial) taking care about spawning on rand points (and working for me as expected), maybe you can get a bit a direction:

private NetworkPlayer localPlayer;
private NetworkViewID localTransformViewID;
private bool isInstantiated = false;

public void OnPlayerConnected (NetworkPlayer playerNID) {
	NetworkViewID transformViewID = Network.AllocateViewID();
	networkView.RPC("InitPlayer", playerNID, playerNID, transformViewID);

void InitPlayer (NetworkPlayer playerNID, NetworkViewID transformViewID ) {
	localPlayer = playerNID;
	localTransformViewID = transformViewID;

void Update() {

	if (Network.isClient  && !isInstantiated) {
		GameObject[] spawnPointsInScene = GameObject.FindGameObjectsWithTag("SpawnPoint");
		int idx = Random.Range(0,spawnPointsInScene.Length);
		currentSpawnPoint = spawnPointsInScene[idx].transform.position;
		networkView.RPC("SpawnPlayer", RPCMode.AllBuffered, localPlayer, localTransformViewID, currentSpawnPoint);
		isInstantiated = true;

void SpawnPlayer (NetworkPlayer playerNID, NetworkViewID transformViewID, Vector3 currentSpawnPoint) {

	instantiatedPlayer = Instantiate(playerPrefab, currentSpawnPoint, transform.rotation) as Transform;

Probably, depending by your other rules in networking, it’s about RPCMode and Network.isClient/Network.isServer (or networkView.isMine) to not take rand() as different on each client when a client ask for instantiating.