Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Can someone explain how TargetRPC works?

Discussion in 'Multiplayer' started by Durins-Bane, Jan 30, 2017.

  1. Durins-Bane

    Durins-Bane

    Joined:
    Sep 21, 2012
    Posts:
    175
    Im trying to get my server to connect to the client that last hit the mob and then execute a function on that client only

    So I have my game setup and my enemies are spawned by the server. They carry the variable that holds the object of the last player that hit it.on the server.

    So once the enemy dies on the server it should find that players object and then tell that client only to execute a function which increases its exp

    How do I do it using this?
    https://docs.unity3d.com/ScriptReference/Networking.TargetRpcAttribute.html
     
  2. Durins-Bane

    Durins-Bane

    Joined:
    Sep 21, 2012
    Posts:
    175
  3. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    650
    You might be better off with a [SyncVar] exp variable on each player. Then increase the value on the server, and it will trickle down to the client(s).

    But if you want to use TargetRPC then on the player that killed the monster call your TargetRPC method on the server. Pass the networkConnection of the owner of that player. Then the TargetRPC will only be called on the client owner of that player.
     
  4. Durins-Bane

    Durins-Bane

    Joined:
    Sep 21, 2012
    Posts:
    175
    How do I use the targetRPC method?

    https://docs.unity3d.com/ScriptReference/Networking.TargetRpcAttribute.html
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3.  
    4. public class Example : NetworkBehaviour {
    5.  
    6.     [TargetRpc]
    7.     public void TargetDoMagic(NetworkConnection target, int extra)
    8.     {
    9.         Debug.Log ("Magic = " + (123 + extra));
    10.     }
    11.  
    12.  
    13.     [Command]
    14.     void CmdTest()      
    15.         TargetDoMagic(connectionToClient, 55);
    16.     }
    17.  
    18. }

    Here is what I tried but I am getting an error

    I have this on the enemy that is controlled by the server

    Code (CSharp):
    1.   [TargetRpc]
    2.     public void TargetDoMagic(NetworkConnection target)
    3.     {
    4.  
    5.  
    6.                 int amountToGive = Random.Range(minExp, maxExp);
    7.                 //ADJUST EXP ON CHARACTER THAT LAST HIT
    8.                
    9.                 //   NetworkServer.SendToClient(playerID, sInt, );
    10.                 playerLastHit.recieveExp(amountToGive);
    11.            
    12.         }
    13.  
    14.  
    15.     [Command]
    16.     void CmdGIVEEXPv2()
    17.                 {
    18.                     if (PlayerLastObject != null && isServer)
    19.                     {
    20.                         playerLastHit = PlayerLastObject.GetComponent<PlayerConfig>();
    21.                         if (playerLastHit != null) {
    22.                 //   int playerID = playerLastHit.GetComponent<NetworkConnection>().connectionId;
    23.                          
    24.  [I][B]               TargetDoMagic(playerLastHit.GetComponent<NetworkConnection>());[/B][/I]
    25.                         }
    26.                     }
    27.     }
    28.  
    bold line is where I get the error

    Code (CSharp):
    1. ArgumentException: GetComponent requires that the requested component 'NetworkConnection' derives from MonoBehaviour or Component or is an interface.
    2. UnityEngine.Component.GetComponent[NetworkConnection] () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/ComponentBindings.gen.cs:45)
    3. Combat.CmdGIVEEXPv2 () (at Assets/_SCRIPTS/Combat.cs:48)
    4. Combat.CallCmdGIVEEXPv2 ()
    5. Combat.TakeDamage (Int32 amount) (at Assets/_SCRIPTS/Combat.cs:87)
    6. Bullet.OnCollisionEnter (UnityEngine.Collision collision) (at Assets/_SCRIPTS/Bullet.cs:12)
    7.  
     
  5. SyncStelar

    SyncStelar

    Joined:
    Aug 17, 2017
    Posts:
    1
    I know this post is a bit old but I can't help to reply here.

    The reason why this here isn't working is because you tried to use GetComponent on a non-monobehavior script and is in fact, IDisposable.

    https://bitbucket.org/Unity-Technol...ection.cs?at=5.3&fileviewer=file-view-default