- Home /
Pass a Transform into a Function
hey im trying to pass a transform.position over a network via a rpc call but then even i try to call the function over the network nothing happends
this is on the script of my gun
h.GetComponent<PhotonView>().RPC ("TakeDamage", PhotonTargets.AllBuffered, bulletDamage, bulletOwner.transform));
this is the function on my health script "h"
[RPC]
public void TakeDamage(float amt, Transform dmgByPlayer){
currentHitPoints -= amt;
Debug.Log(dmgByPlayer);
if (currentHitPoints <= 0)
{
//killedBY = dmgByPlayer;
Die();
}
}
any ideas on how to get this working
Answer by whydoidoit · Mar 11, 2014 at 10:29 AM
You can't pass a transform, but you can pass the position. If you want to pass a reference to the player you are best off sending an ID (such as the NetworkViewID) for the player and using that to work out which player it is.
how would i get the id and then find a player with that id. an example would be good using the PhotonNetwork that is
Ok so maybe the easiest way is to attach a script like this to every player:
PlayerIdentity.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerIdentity : $$anonymous$$onoBehaviour {
public string id = System.Guid.NewGuid().ToString();
public static Dictionary<string, PlayerIdentity> lookup = new Dictionary<string, PlayerIdentity>();
void OnEnable() {
lookup[id] = this;
}
void OnDisable() {
lookup.Remove(id);
}
}
Now you can get the ID from a player by doing:
var id = someTransformOrComponent.GetComponent<PlayerIdentity>().id;
And given an id you can get the object:
var somePlayer = PlayerIdentity.lookup[idYouWereSupplied];
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Does RPCMode.All perform poorly? 1 Answer
Send array of transforms via RPC. [C#] 1 Answer
Rpc client issue 0 Answers