• 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 /
avatar image
Question by stratos_kakalis · Dec 29, 2017 at 06:29 PM · networkingserverclient

Standard function on networkBehaviour called on the server?

I must be missing something simple but I havent been able to find an answer so bear with me...

I have a script which is called from a [Command] function and inside has the PlayerDie funtion. (this script is on the player with the local authority).

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Networking;
 using UnityEngine.UI;
 
 public class PlayerHealth : NetworkBehaviour {
 
     [SyncVar] int health = 100;
 
     public GameObject deathCam;
 
     public void TakeDamage(int damage)
     {
         health -= damage;
         if (health <= 0)
         {
             PlayerDie();
         }
     }
 
     void PlayerDie()
     {
         Instantiate(deathCam, transform.position, Quaternion.identity);
         NetworkServer.Destroy(gameObject);
     }
 
 }
 


As you can see I am Instantiating a gameObject on that function and also destroying the player gameObject. The destruction happens on all the clients and the Instantiation happens only on the server even when the local client isn't the host. My question is why? From my understanding simple void functions should be called localy like a monoBehaviour script but I am noticing this getting called on the server instead although its not a command. As a result i am seeing the "deathCam" object get spawned on the host and not the client who "died".

Comment

People who like this

0 Show 0
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

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by mossflower · Dec 29, 2017 at 06:46 PM

If you want to have the clients do something, you have to make it an RPC:

 [TargetRpc]
 void TargetPlayerDie(NetworkConnection connection)
 {
     Instantiate(deathCam, transform.position, Quaternion.Identity);
 }

Also, you probably want to Destroy from the Server:

 [Command]
 void CmdOnServer() { ...
 playerHealth.TargetPlayerDie(playerHealth.connectionToClient);
 NetworkServer.Destroy(playerHealth.gameObject);
Comment
stratos_kakalis

People who like this

1 Show 4 · Share
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
avatar image stratos_kakalis · Dec 30, 2017 at 07:22 AM 0
Share

Yes, but that's what buggs me. Although I am not using a command I am getting that function called on the server. (doesn't make sense I am going to make sure that's the case). Also I don't want an Rpc call because I only want the gameobject with the script on the local client to update not necessarily for all the players.

avatar image stratos_kakalis · Dec 30, 2017 at 08:40 AM 0
Share

I went back and checked my code and things are happening as i described them. In case i wasnt clear enough i have a function:

 [Command]
 void CmdRandomFunction (){
 targetGameObject.GetComponent<SomeScript>().TakeDamage();
 }

then on the "SomeScript":

 ....
 
 public void TakeDamage(){
 PlayerDie();
 }
 
 void PlayerDie(){
 Instantiate (something, somewhere, withSomeRotation)
 }
 
 ....

the Instantiation happens on the server. Is it because the script is ultimately being called by a command? And how could i call it localy...?

avatar image mossflower stratos_kakalis · Dec 30, 2017 at 05:21 PM 0
Share

Instead of thinking of things as happening "locally", it helps to always think of something as either happening on the server or happening on the client. Health should be managed on the server, and when a player dies, the client for that player should be sent a message saying they died. You can use [TargetRpc] to make it happen specifically on the client for the player that died, instead of [ClientRpc] which would make it happen on all clients. I think your "SomeScript" should look like this:

 ....
 
 public void TakeDamage(int damage) {
     if (!isServer)
         return;
     health -= damage;
     if (health <= 0) {
         TargetPlayerDie(connectionToClient);
         NetworkServer.Destroy(gameObject);
     }
 }
 
 [TargetRpc]
 void TargetPlayerDie(NetworkConnection connection) {
     Instantiate(something, somewhere, withSomeRotation);
 }

 ...
avatar image stratos_kakalis mossflower · Dec 31, 2017 at 10:59 AM 0
Share

Oh i didn't understand in your previous answer you suggested a TargetRpc I only knew the ClientRpc function. I guess it can't work the way I imagined but this is the closest workaround. Thanks for the help, this works!

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

106 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

Related Questions

Unity networking tutorial? 6 Answers

How to check when a new client/local player is loaded? 1 Answer

Connect to Server on different computer (local network) 0 Answers

How do I change networkView ownership of a Rigidbody by clicking on it? 1 Answer

"Trying to send command for object without authority" problem. 1 Answer


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