Unity multiplayer tutorial - Adding a respawn button
I followed the Multiplayer tutorial that unity offers on their website and I have everything working correctly. I would like to make it so that when a player dies a respawn button appears and by clicking it the player respawns at one of the locations I have specified.
I have the respawn button in my scene and I have it so it appears when a player dies. My problem is that I do not know how to correctly call the respawn function. The respawn function is a [ClientRpc] function and when I click the respawn button from the client I get the error "Rpc function called on client" because [ClientRpc] sends data from the server to the client, not client to server.
The strange thing is that if I call my respawn function in my TakeDamage() function (see below), everything works fine except there is no respawn button involved. If you have a workaround or any advice let me know, I feel like I've tried everything at this point. Thanks!
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
public class Health : NetworkBehaviour {
public const int maxHealth = 100;
public bool isDead = false;
public bool destroyOnDeath;
public GameObject model;
[SerializeField] GameObject hbar;
public RectTransform healthBar;
private NetworkStartPosition[] spawnPoints;
private static Quaternion spawnRot = Quaternion.Euler (0, 0, 0);
private GameObject respawnBtn;
private NetworkIdentity objNetId;
[SyncVar(hook = "OnChangeHealth")]
public int currentHealth = maxHealth;
void Start()
{
// Initialize model
model = gameObject;
// Initialize respawn button
respawnBtn = GameObject.Find ("RespawnButton");
respawnBtn.GetComponent<Button> ().onClick.AddListener (RpcRespawn);
respButtonHidden ();
// Initialize spawn points
if (isLocalPlayer) {
spawnPoints = FindObjectsOfType<NetworkStartPosition>();
}
}
public void TakeDamage(int amount)
{
// Makes sure damage is only applied to server
if (!isServer) {
return;
}
// Decreases the health by a defined amount
currentHealth -= amount;
// If health is <= to 0, kill the player
if (currentHealth <= 0)
{
isDead = true;
RpcPlayerDied ();
//RpcRespawn ();
}
}
// Resests player's health
public void ResetHealth() {
isDead = false;
currentHealth = maxHealth;
}
// Make respawn button visible
void respButtonActive() {
respawnBtn.GetComponent<Image> ().enabled = true;
respawnBtn.GetComponent<Button> ().enabled = true;
respawnBtn.GetComponentInChildren<Text> ().enabled = true;
}
// Make respawn button hidden
void respButtonHidden() {
respawnBtn.GetComponent<Image> ().enabled = false;
respawnBtn.GetComponent<Button> ().enabled = false;
respawnBtn.GetComponentInChildren<Text> ().enabled = false;
}
// Update's player's health bar
void OnChangeHealth (int health)
{
healthBar.sizeDelta = new Vector2 (health, healthBar.sizeDelta.y);
}
// Disables and hides the player. Respawn button activates.
[ClientRpc]
void RpcPlayerDied() {
if (isLocalPlayer) {
isDead = true;
respButtonActive ();
}
model.SetActive (false);
hbar.SetActive (false);
gameObject.GetComponent<Rigidbody> ().detectCollisions = false;
gameObject.GetComponent<PlayerController> ().enabled = false;
}
// Repositions the player and makes it visible. Respawn button disables.
[ClientRpc]
public void RpcRespawn()
{
// Make player visible
model.SetActive (true);
hbar.SetActive (true);
model.GetComponent<Rigidbody> ().detectCollisions = true;
isDead = false;
ResetHealth ();
model.GetComponent<PlayerController> ().enabled = true;
if (isLocalPlayer) {
respButtonHidden ();
// Set the spawn point to origin as a default value
Vector3 spawnPoint = Vector3.zero;
// If there is a spawn point array and the array is not empty, pick one at random
if (spawnPoints != null && spawnPoints.Length > 0) {
spawnPoint = spawnPoints [Random.Range (0, spawnPoints.Length)].transform.position;
}
// Set the player’s position to the chosen spawn point
transform.position = spawnPoint;
transform.rotation = spawnRot;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612135809im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple mobile devices 0 Answers
Photon RPC Only Working Halfway 1 Answer
Updating Multiplayer Tutorial with VR 1 Answer
Unity Networking: Loading Screen 0 Answers