Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by ntbsweeps · Sep 28, 2017 at 03:09 AM · buttonmultiplayertutorialrpcrespawn

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;
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

175 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do i use RPC's to set a object active in the hierarchy for all Players in the room and also how do i synchronize UI events focused on a VideoPlayer (Play, Pause etc.)) 0 Answers

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges