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
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.
using UnityEngine;
using UnityEngine.Networking;
public class Example : NetworkBehaviour {
[TargetRpc]
public void TargetDoMagic(NetworkConnection target, int extra)
{
Debug.Log ("Magic = " + (123 + extra));
}
[Command]
void CmdTest()
TargetDoMagic(connectionToClient, 55);
}
}
Here is what I tried but I am getting an error
I have this on the enemy that is controlled by the server
[TargetRpc]
public void TargetDoMagic(NetworkConnection target)
{
int amountToGive = Random.Range(minExp, maxExp);
//ADJUST EXP ON CHARACTER THAT LAST HIT
// NetworkServer.SendToClient(playerID, sInt, );
playerLastHit.recieveExp(amountToGive);
}
[Command]
void CmdGIVEEXPv2()
{
if (PlayerLastObject != null && isServer)
{
playerLastHit = PlayerLastObject.GetComponent<PlayerConfig>();
if (playerLastHit != null) {
// int playerID = playerLastHit.GetComponent<NetworkConnection>().connectionId;
[I][B] TargetDoMagic(playerLastHit.GetComponent<NetworkConnection>());[/B][/I]
}
}
}
bold line is where I get the error
ArgumentException: GetComponent requires that the requested component 'NetworkConnection' derives from MonoBehaviour or Component or is an interface.
UnityEngine.Component.GetComponent[NetworkConnection] () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/ComponentBindings.gen.cs:45)
Combat.CmdGIVEEXPv2 () (at Assets/_SCRIPTS/Combat.cs:48)
Combat.CallCmdGIVEEXPv2 ()
Combat.TakeDamage (Int32 amount) (at Assets/_SCRIPTS/Combat.cs:87)
Bullet.OnCollisionEnter (UnityEngine.Collision collision) (at Assets/_SCRIPTS/Bullet.cs:12)