- Home /
Photon Network variables on spawn not set on client
I have the following method to spawn a GameObject (which is being fired on RMB).
void SpawnProjectile(string SpellName)
{
Vector3 spawnLocation = transform.Find("ProjectileSpawnLocation").transform.position;
GameObject go = (GameObject)PhotonNetwork.Instantiate(SpellName, spawnLocation, transform.rotation, 0);
Projectile projectile = go.GetComponent<Projectile>();
projectile.totalRange = projectile.BaseRange + spell.getSpellLevel();
projectile.totalDamage = projectile.BaseDamage + spell.getSpellLevel();
go.name = SpellName;
projectile.Instigator = gameObject;
projectile.enabled = true;
}
This works fine for the master client, however, when I do this on a client, variables aren't right and it gives errors like e.g. Null reference on Instigator, because I'm checking on the OnCollisionEnter if the Instigator is not equal to the collision object.
What can I do to make this work for all connected clients? Thanks in advance!
Answer by alexi123454 · Jul 21, 2015 at 01:54 PM
You're only setting the variables on the master client, because this function is only being called on the master client. To send the same information to the rest of the players in the game, you're going to have to send them a network message (an RPC).
You can create an RPC function by putting the [PunRPC] tag above it, and then calling it on the master client by calling GetComponent().RPC("rpcFunctionName", PhotonTargets.Others, spellName, totalRange, totalDamage, SpellName, instigator.GetComponent().owner), or something along those lines.
Just send the variables you want to set as arguments through the RPC call, and all the other clients (or all clients including the master client if you use PhotonTargets.All instead) will have all of the variables, and can set the projectile's variables themselves.
I tried using a RPC call before (obviously in a wrong way), there is one thing I dont get though. I used a new method purely to set variables for the projectile via RPC, this works fine, but I don't quite get how to assign the Instigator (owner) to the projectile since I can't pass Transform and GameObject as an argument.
Ins$$anonymous$$d of passing the transform and the gameObject, just pass the instigator.GetComponent().owner. This is an int that identifies a PhotonPlayer. Once on the client side, you can search through the players in the scene, and see if their owner matches the owner received in the RPC call. If it does, set the instigator to that gameObject!