Search Unity

Unity Multiplayer [SOLVED][ClientRpc] how it's work on non-player object?

Discussion in 'Multiplayer' started by Rafael_Gratival, Aug 20, 2016.

  1. Rafael_Gratival

    Rafael_Gratival

    Joined:
    Oct 27, 2014
    Posts:
    9
    Hello someone could I explain in detail how the [ ClientRpc ] , I have read the page on , but I still do not fully understand. With this code 'm down trying to register an object that has just become active and passes it to all client's. In my attempt's the server side does not have errors when activating the object but on the client side keeps popping up an error does not access the netID .


    Setup: The object with this script has a child (the _newObject) disabled on online scene.


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System;
    4. using System.Collections;
    5.  
    6. public class MainSystem : NetworkBehaviour {
    7.    
    8.     #region Starter
    9.     public GameObject _newObject;
    10.     bool OneTime = false;
    11.     #endregion
    12.  
    13.     [ClientRpc]
    14.     void RpcRegister(GameObject obj)
    15.     {
    16.         ClientScene.RegisterPrefab (obj);
    17.     }
    18.        
    19.     void Update ()
    20.     {
    21.         if (!isServer)
    22.             return;
    23.          
    24.         if(!OneTime)
    25.          {
    26.               _newObject.SetActive(true);
    27.               NetworkServer.Spawn (_newObject);
    28.               RpcRegister(_newObject);
    29.               OneTime = true;
    30.          }
    31.      }
    32. }
    33.  
     
  2. bazzer

    bazzer

    Joined:
    Nov 9, 2012
    Posts:
    7
    Gameobjects can not be passed around using an RPC due to the way UNet works. Instead you can send a text description or int ID to your clients using an RPC and have the client load the correct GameObject from there (for example from your Resources folder).
    See link for someone with a similar situation.
    Also note that any NetworkIdentity must reside on the parent GameObject for it to be able to communicate across the network.

    Hope this helps.
     
    pKallv likes this.
  3. Rafael_Gratival

    Rafael_Gratival

    Joined:
    Oct 27, 2014
    Posts:
    9
    @bazzer thanks for the awsner i manage to get how it works!
     
  4. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    946
    can you share the working code?
     
  5. Rafael_Gratival

    Rafael_Gratival

    Joined:
    Oct 27, 2014
    Posts:
    9
    @pKallv sorry for the late answer.

    http://asperatology.me/blogs/Sharing-tips/ this site help`s me to understand a bunch of things from server and client,

    plus here is a script for non player object . Sync & Interacting with player.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.Networking;
    4. using System.Collections;
    5.  
    6. public class ExampleScript : NetworkBehaviour {
    7.  
    8.     #region Declaration
    9.     private int Health;
    10.     private float Damage;
    11.  
    12.     public NetworkIdentity ExampleIdentify; // this one that i mention on down topic.
    13.  
    14.     [SyncVar]
    15.     public Vector3 Position;
    16.     [SyncVar]
    17.     public Quaternion Rotation;
    18.  
    19.     #endregion
    20.  
    21.     public override void OnStartServer()
    22.     {
    23.         Health = 100; // here put your life as your wish;
    24.         Damage = 25;  // same as top line.
    25.         ExampleIdentify = this.gameObject.GetComponent <NetworkIdentity>(); // it will identify as uniq on network and will be easy to look up to it.
    26.     }
    27.  
    28.     [ServerCallback]
    29.     public void FixedUpdate()
    30.     {
    31.         if (!isServer) //Only server will acess at this point to down;
    32.             return;
    33.  
    34.         Position = transform.position; // this will sync Position across all clients at runtime your gameObject that has this script attach;
    35.         Rotation = transform.rotation; // this will sync Rotation across all clients at runtime your gameObject that has this script attach;
    36.  
    37.         // Here you put everthing that your non-playerObject want to do atack, life , stats ...
    38.  
    39.         // Here is just a sample not automatic for Server Pass Info to player.
    40.         /*
    41.         * if(Condition)
    42.         {
    43.             NetworkIdentity Player = (NetworkIdentity)GameObject.Find ("Player").GetComponent <NetworkIdentity> ();
    44.             GiveDamage (Damage,Player);
    45.         }
    46.         */
    47.     }
    48.  
    49.     //Receiving damage from Player its simply, create a path to this script call this function and apply Damage to its health;
    50.     public int TakeDamage(int DamageReceive)
    51.     {
    52.         Health -= DamageReceive;
    53.  
    54.         return Health; // return life now;
    55.     }
    56.  
    57.     //Here becomes a little complex but not to worry, mine preference is to send via NetworkIdentity, because works for me, but the correctly way is by NetWorkConnection.
    58.     public float GiveDamage(float DamageSend,NetworkIdentity Identify)
    59.     {
    60.         //And if you think "how i know this NetworkIdentify", in each script that you put on enemy or player i recommend
    61.         //you create a variable to hold your Identify like this one; this function ReceiveDamage just there by example , create this on <ScriptOnThisIdentifyObject>.
    62.  
    63.         /*GameObject PlayerX = Identify.gameObject.GetComponent <ScriptOnThisIdentifyObject> ().ReceiveDamage (DamageSend);*/
    64.  
    65.         return Damage; // just to see the result.
    66.     }
    67. }
    68.  
     
  6. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    946
    Thanks @Aruaca

    i have something similar that I use, dunno where i found it:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class MP_explanations : NetworkBehaviour {
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.  
    10.         if (isServer) {
    11.             //It's the server
    12.         }
    13.  
    14.         if (isClient) {
    15.             //It's a client
    16.         }
    17.  
    18.         if (isLocalPlayer) {
    19.             //is the actual player
    20.             ClientFunction();
    21.         }
    22.  
    23.  
    24.     }
    25.  
    26.     [Client] void ClientFunction() {
    27.         print("I am the client");
    28.         CmdRunOnServer();
    29.     }
    30.  
    31.     [Command] void CmdRunOnServer() {
    32.         print("The client called me to run on the server.");
    33.         ServerFunction();
    34.     }
    35.  
    36.     [Server] void ServerFunction() {
    37.         print("I am the server, I handle the important decisions.");
    38.         RpcRunOnClient();
    39.     }
    40.  
    41.     [ClientRpc] void RpcRunOnClient() {
    42.         print("The server called me to run on the client");
    43.     }
    44. }
     
unityunity